One of the best ways to prevent that moldy smell from invading a clothes washer is to run the machine empty, using chlorine bleach or simply the hot water setting. Powdered laundry detergent also works. Do this once or twice a month, and the mold problem should be solved.
Front load washers are more prone to the moldy smell because water can pool under the bottom of the drum and doesn't evaporate as fast. Some front loaders have a special cleaning setting that can be used for the empty load washes described above. Another feature is a fan-dry setting that blows air on a wet load until it is removed from the drum.
Other helpful tips include wiping down the door gasket and the inside of the glass after use, monthly cleaning of the detergent dispenser and using a dehumidifier in the laundry room. Leaving the washer door ajar is an option, but not advised if there are pets and/or small children in the house.
If a laundry room has no windows, using a washer tends to increase the humidity levels. Even if the door is left ajar, the increased moisture in the room might prevent airing out of the washer. One solution is to install a laundry room door with louvers, or slats. These movable strips of wood can be adjusted as needed, improving air circulation even when the doors are closed.