How do shows know that you or tv viewers are watching their program?

They hire the Nielsen Company to measure their viewership. Nielsen uses diaries and electonic boxes to measure ratings. (No, you cannot request it, Nielsen decides).