Rusting of iron happens when iron reacts with oxygen and moisture from the atmosphere. This process can be prevented by cutting off the contact between the surface of the metal and atmosphere.
This can be done by methods like painting, oiling, greasing, etc. In these methods, a layer of a different substance is coated on the metal surface preventing the interaction of oxygen and water with the iron surface.