Rusting is the process in which iron in the open air acquires a iayer of a brownish substance called rust. For rusting, iron should be in contact with both air and moisture.
When an iron gate is painted then the layer of paint cuts off the contact between air, moisture from iron. Thus, it prevents rusting. So, when we paint an iron gate it prevents rusting.