Actually there's no exact way to track if user closed browser or went to other site.
The simplest solution is to store each users's session ID with last page access time in database and to get 'online' users use query like:
select count(*) as users_online from sess_table where (unix_timestamp()-access_time) < 600
access_time here is in Unix time() format.
Also don't forget to purge old sessions (where access_time is more than a hour in past, for example).