It involves a redox reaction and formation of an electrochemical cell on the surface of iron or any other metal. At one location oxidation of iron takes place (anode) and at another location reduction of oxygen to form water takes place (cathode). First Fe gets oxidized to Fe2+ and then in the presence of oxygen, it forms Fe3+ which then reacts with water to form rust which is represented by Fe2O3.xH2O.
Rusting of iron can be avoided by painting it or by coating it with some other metals like Zinc. The latter process is known as Galvanization. As the tendency of Zn to get oxidized is more than iron it gets oxidized in preference and iron is protected. This method of protecting one metal by the other is also called Cathodic Protection.