For rusting, the presence of both oxygen and water (or water vapour) is essential. In fact, if the content of moisture in air is high, which means if it is more humid, rusting becomes faster.Therefor painting of an iron gate prevent iron from coming in contact with oxygen, or water, or both hence prevent it from rusting.
Rusting is a chemical change in which the iron reacts with the oxygen to form iron oxide. It occurs when an iron is exposed to the air and it reacts with the oxygen present in the air. To prevent this rusting paint is applied. This paint provides a coating of a protective layer which prevents the iron from further reaction with the oxygen present in the air.