Every water-using fixture in a building is an endpoint to the city water system (or a private water system, in well-fed buildings) and an origin point of a sewer or septic system. The problem with this fact - which greatly slowed the spread of indoor plumbing in the late 18th and early 19th centuries - is that gasses from the sewer system tend to rise and spill out of fixtures connected to it. Anyone who has encountered these gasses can attest to their vile and unhealthy nature, and in the early days of plumbing they could render whole buildings uninhabitable.

A number of solutions were attempted throughout the years, but the most successful (and thus most common today) is the trap, a sideways-S-shaped pipe that - you guessed it - traps the gasses and prevents them from making their way up through the fixture. Basically, there's a dip in the pipe just below the fixture (half of the "S" shape), after which it the pipe turns upwards again (to form the other half of the "S".) This "traps" water in the dip; this water seal prevents the exchange of gasses between the sewer and the air of the building.

Here's some bad ASCII art, which might make things a little clearer:


                               ^
                               |  
                          to fixture
                               
                     ___      | |
                    / _ \     | |
                   / / \ \    | |
awful stench -->  / /   \ \   | |
                  | |   |-|   |-|
                  | |   \ \   / /
                  | |    \ \_/ /  <-- water
                  | |     \___/

                   |
                   V
               to sewer

You'll recognize traps under sinks in your home, where they're plainly visible, but they're also present in any other fixture connected to the sewer system. They may be concealed inside the fixture, behind a wall or under the floor, as in tubs and showers, but trust me - they're there. You'd know if they weren't.

Early traps were problematic - the siphon effect of water flowing into the sewer tended to pull the water seal down with it, or back pressure in the sewer lines would blow the water seal back into the fixture (yech.) In 1874, however, an unidentified but clever plumber realized that the pressure in the sewer-side of the system could be equalized with normal atmospheric pressure by means of a vent pipe. Modern plumbing systems all include a venting system that aggregates the vents from all of the traps in the system to an output (called a "stack vent" or "stink pipe," depending on who you talk to) that can be routed to an inoffensive location (usually the roof). Leaks in this venting system can release foul odors; alternately, if it clogs then traps may lose their water seals and begin to release gas themselves.

The water seal in infrequently-used fixtures may eventually evaporate, with unpleasant results. If a disused sink or tub starts to smell bad, that's probably the cause - just run some water down it to re-establish the seal in the trap.

Sources: Kentucky State Plumbing Code, 815 KAR 20:010 (Section 1, "Definition of Terms,") http://www.lrc.state.ky.us/kar/815/020/010.htm
"Plumbing Care & Repair Handbook," http://www.theplumber.com/handbook.html
"The New World: Plumbing History," http://www.plumbingstore.com/psamerica.html