Air and surfaces are filled with random mould spores and bacteria. If they touch your beverage (which is by definition full of stuff that mould loves, because yeast is a mould) then they will grow, causing off-flavours or even poisons. You prevent this by sanitising everything, and making sure that any headspace is filled with either carbon dioxide or sulphur dioxide. CO2 is produced naturally by fermentation and because it's heavier than air it settles in the headspace displacing oxygen (moulds need oxygen to reproduce). Sulphur dioxide can be added using sterilising tablets (Campden tablets). Other techniques are to reduce the headspace by adding more liquid, or marbles, apparently, and using an airlock to stop air entering.