The transponder is a box full of circuitry in the airplane, connected to an antenna. There are fancy transponders that transmit all kinds of parameters, but your standard Mode C transponder transmits only the pressure altitude (it's gets the number from the encoding altimeter) and the four digit code that is entered on the transponder. You enter it either by turning a knob for each digit (the old kind) or by punching the four numbers on a keypad (the new kind).
The four digit code is assigned by the ATC unit, usually just the next sequential one in the block that that unit controls. So the pilot puts 1254 on his box and the controller enters the details about that airplane on her computer. Now when the controller looks at her screen, the blip to which she has assigned 1254 is identified by call sign, destination, groundspeed and altitude.
There are a number of default transponder codes, for pilots to use when they haven't been assigned anything else, and there are a few codes that signify a special message. When the controller's radar computer detects an airplane squawking (that's the proper verb) one of those codes it sets off an alarm, and the controller knows that an airplane has a problem even if the pilots can't transmit that information. For example, 7600 signifies a communications failure and 7700 signifies an emergency. Sometimes controllers and pilots will use the transponder as a means of communication. E.g. "Aircraft north of PLACE squawking 7600, if you receive this transmission squawk 2522." It's common for an aircraft to have a radio problem that renders it able to receive but not transmit.
This is background to a story a reader told me about an exhausting sim session during which the instructor piled on emergency after emergency. The pilot had, if I recall correctly, just taken off from an airport in poor weather conditions and been handed an engine failure and a communications failure. After handling the engine failure according to the checklist, the pilot now needs to return for landing. If he had only the communication failure, there is an established procedure to follow, squawking 7600, following any published airport-specific comm failure procedures, and proceeding en route with altitudes and approaches according to established rules. But with one engine out, he's hardly going to continue to destination. If it were an engine failure alone, he would report that and tell the tower he was coming back for landing on runway 22. But with both, he's a little bit screwed. Just a little bit, though, because he came up with a clever plan.
He set the transponder to:
7600 - just long enough for the tower to recognize the comm failure
Then to 7700 - so they could see it was also an emergency. At this point the controller is probably picking up a phone and or calling across the room to colleagues, about the situation, but not knowing what the emergency is, has no idea what the airplane is about to do. So far that's what any pilot would do in this situation.
Then the pilot changed the code again, to 0022. In the telling of the story I recognized it immediately as informing ATC that he would be returning for landing on runway 22.
In the simulator, the instructor understood it too, saw that the pilot had this extreme situation under control, and thus said, "we're done." And the pilot got to keep his job for another six months.
I thought it was clever enough to add to my bag of tricks and to share with you.