P-traps, the curving section of pipe you can see under the sinks in your house, perform a vital function for any plumbing fixture in a building. Shower and bathtub drain pipes must incorporate a P-trap; otherwise the health and safety of anyone in the building will be jeopardised by sewer gasses entering the building.
Sewer gasses not only smell bad, especially if they invade your home, but they also can harm the health of anyone who inhales them for an extended period. On top of the health risks associated with inhaling sewer gasses, the gasses can also contain methane, a gas that will combust when exposed to open flame. Sewer gasses in a home puts everyone in the home at risk.
A P-trap provides protection from sewer gasses coming into your house. The sewer gasses would travel up the drainpipes from the sewer and eventually exit out the tub or shower's drain if no device were in the way. A P-trap will stop the sewer gasses from coming further up the drain line. Because of the curved shaped of a P-trap, the trap ideally will be full of water at any given time. The sewer gasses will rise to the trap but will not travel through the water.
The vent pipes in your home play as important a role in keeping sewer gasses out as the P-traps. The sewer gasses that cannot escape through the P-trap will instead go up the vent pipe, which exits out of the house's roof. For homes that have a septic tank, this venting will prevent a build-up of gasses in the septic tank, which will also put the occupants of the house at risk of an explosion.
A P-trap will fail in its function of blocking sewer gasses from coming up through a tub or shower's drain if the trap does not stay full of water. Leaks in the trap or the connections on either side of the P-trap can empty enough water from the trap that it no longer effectively blocks sewer gasses. If nobody uses the bathtub or shower often enough, the water will eventually evaporate out of the trap. How quickly the water evaporates will depend on the humidity levels in your area.