Every drain in your home plumbing system should have a trap installed as part of the line. This is simply a curved section of pipe that “traps” water in it. If you look under your sink you should see an example of a trap. This will usually be a straight portion of pipe from the sink connected to a curved section that dips down and then back up before connecting to another horizontal section where the water exits. If you were to look inside your wall you would see a “T” section where the pipe goes down to your septic/sewer and up to an external vent.
The purpose of the trap is not to capture jewelry or other object dropped down the drain as many think but to hold water to prevent sewer/septic gas (and odor) from entering your bathroom. Every time you use the sink, shower, or tub some water collects in the trap blocking the gases and odor. If a component of your bathroom is not used frequently (i.e, the bath in a guest bathroom) then the water can evaporate and allow some gases to escape back into the bathroom. The good news is that this is very simple to prevent and/or fix. The solution: pour water down the drain. If you keep the trap full of water by running the water periodically you should not have any problems with odor. If this was the cause of your problem then the odor should dissipate in a short period of time once there is water in the trap.
Another possible cause for odor is the build up of slime, mold, hair, etc in the tailpiece (the portion of pipe that comes out the bottom of your sink) or overflow of your sink. The popup stopper in many bathroom sinks and tubs can also build up this “gunk”. Use a small flashlight to check and see if you do have any buildup. If you do, clean the buildup out (remember to use a cleaner that is safe for your sewer or septic system) and see if that addresses your odor problem.