A rainbow is formed in the sky due to dispersion of white light. When a light ray passes from one medium to another medium it bends away or towards the normal depending on the refractive index of the two media. Now, light of different wavelength gets deviated at different angle when they go from one medium to another. So, when light passes to water droplets in the air, the different colours of light bends at different angles due to their difference in wavelength and we see a rainbow.