Contrary to popular belief, they don't have some kind of national detector! A selected number of households (presumably a representative cross-section) have a device installed which monitors their viewing, and the ratings are extrapolated from this sample. However, I'm not sure why they do all this, because there's never anything decent on TV!!!
No the real answer is that they have x amount of people at any time filling out surveys on what tv or radio they listen to or watch. From this they estamate how many people are watching. Simple but true