When white light from the sun shines through tiny rain drops suspended after rainfall,like tiny prisms.They refract and reflect the sunlight and then separate it into different colours.
A rainbow is formed due to 3 fundemental processes in optics called Refraction, Reflection and Dispersion.
Refraction is the curvature of a wave (in this case visible light) when it passes through a transparent surface.
Reflection is the “Bouncing back” of the wave.
Dispersion is the seperation of colours according to their wavelength.
The process starts off with a drop of water (usually a raindrop) and Refraction and reflection (mental note: this is why rainbows form after rain, and in sunlight). When light hits the rain drop, some of it reflects off of the raindrop while the rest refracts (passes through) the raindrop.
After the refracted light has passes through the raindrop and reached the other side of it, once again some of the light reflects, and the rest refracts. This time though, the refracted light disperses in order of wavelength, red being the longest. This results in the ordered colour of the rainbow.
A rainbow is not something that physically exists. When viewed from different angles the rainbow moves with you