They do it a bunch of different ways. The "old school" ways are those of the BBM which requires the keeping of a written diary and the other one was to have a box, like a digital set-top box, that actually records what you're watching and for how long. You can imagine the problems with these methods. Nowadays they'll do other kinds of interviews and surveys that ask more detailed questions about what you are doing at the same time as the TV was on.
You can imagine too that it gets hairier as they try and distinguish between other TV-like watching. Streaming through computer, video games, mobile video streams, DVD versus broadcast TV, PVRs, etc. The short answer is, they make it up - it is based upon surveys and assumptions however. Some are better than others, there's no objective standard. It's more what advertisers agree to believe.