Well i believe it goes a little something like this. Don't quote me on this, the method described is how i expect it to be done, not 100% sure.
Using DB:
You could have a table that keeps track of all the people online, but people would have to log off for the database to see that they've gone online. It wouldn't take into account that you could close the browser to go offline like you can with session values, it would assume you were still online.
Using Script:
When you load a page of the forum for the first time in a session some code adds a 1 to the number of people online and a session value called SessionGuest (for instance) is set on your pc. Whilst that session value is true, you will be considered online and you will not add 1 each time you load a page.
The session gets updated each time you load a page and will timeout after a period of inactivity. When it times out, a 1 is subtracted from the number of people online and your session is set to false.
That would tell you how many people are online. For distinguishing between who's a guest and who's a user i guess you could have a session value that is called SessionUser that is set to true when you log in. And if SessionUser is set to true then SessionGuest is set to false for that person.
Seriously that's a rough guess and probably incorrect, but might work..