The answer to 'how does drain trap work' is that a drain trap is a U-shaped pipe section under a sink, tub, or floor drain that holds a small amount of water to prevent sewer gases from entering the home. This standing water creates a seal that blocks odors and pests from coming up through the drain. The trap is designed to maintain this water seal even when the drain is not in use.
The trap works by gravity - when water flows down the drain, it fills the U-shaped portion and leaves a small amount behind. This standing water is what creates the seal. If the trap becomes dry, such as from lack of use, the seal is broken and sewer gases can come back up the pipe. Regularly using the drain or pouring a small amount of water down it helps maintain the water level in the trap.