They're based on the idea that if you're made to tell a lie, your body will react in some way (sweating, faster heartbeat etc) compared to a baseline measurement. There is a whole field of psychology around lying and additionally agents are ostensibly trained to detect visual cues that someone is telling a lie.
The premise isn't exactly junk - hardware lie detectors are basically trying to identify a fight or flight reaction, which some people will exhibit when lying. Have you ever been confronted with a fact (did you eat that cookie?), lied, and felt a rush or a feeling of panic? That's what lie detectors try to detect.
The problem is it's possible to fool a polygraph and it's prone to bias. For example some people get nervous when asked a question, even if innocent. It's theatre in some sense, and it shouldn't be assumed as 100% accurate, but there are plenty of people who will break out into a cold sweat when you ask them a question they don't want to answer truthfully.