Mold does not occur in a vacuum. If you notice mold on your cactus, take a look at the other plants around it. Mold spores can be passed from plant to plant by the breeze; and if there is mold on the cactus, it might have picked it up from nearby specimens. Damp conditions, where water sits on the cactus for long periods of time, may also be a factor.
To prevent mold on your cactus, water it sparingly and, when you do so, water near the base of the plant. This prevents water from splashing onto the upper portion of the plant, where it will not drain away quickly. You should also water your plant first thing in the morning, giving it a whole day in the sun to dry out before cool breezes and nightfall can move in and facilitate mold growth.
Vinegar can be used to kill mold on cacti. While vinegar is very strong and can cause injury to cactus skin, it can also help preserve the plant and keep it alive. Fill a spray bottle with white vinegar and mist a very small amount of vinegar on the mold. The acidic nature of vinegar both kills the mold and prevents it from re-growing.
If an entire segment of your cactus becomes soft and rotten due to mold, there is a chance that the entire plant might die. If you want to save at least part of the cactus, cut away the rotten part with a clean, sharp knife. Cut a large amount of surrounding clean tissue away from the plant as well to prevent a mold infection that may not be readily apparent. Afterward, dust the cut with a systemic fungicide and treat the cactus as you would normally.