A rainbow is formed due to the dispersion of sunlight by tiny water droplets present in the atmosphere after rain.
When sunlight enters a water droplet, it undergoes refraction (bending) at the surface, then internal reflection inside the droplet, and finally refraction again as it exits the droplet.
During these processes, the white sunlight is dispersed into its constituent colors because different colors have different wavelengths and refract by different amounts.
This separation of colors produces the characteristic spectrum of a rainbow, ranging from red (outermost) to violet (innermost).
Hence, dispersion of light by water droplets leads to the formation of a rainbow.