Refraction occurs when light passes from one medium to another and bends due to the change in speed. In a rainbow, light refracts as it enters the water droplets in the atmosphere.
Dispersion is the separation of light into its different colors (spectrum) based on their wavelengths. In the case of a rainbow, different colors of light refract at different angles, creating the spectrum of colors we see.
Total internal reflection occurs when light hits the inside surface of a medium at a steep angle and reflects back into the medium instead of passing through. Inside the water droplets, the light undergoes total internal reflection, which directs the light towards our eyes.
As sunlight enters a water droplet, it first refracts, then disperses into its constituent colors, and finally undergoes total internal reflection. The light then exits the droplet and refracts again as it leaves, creating a rainbow.
Thus, a rainbow is formed by the combined effect of refraction, dispersion, and total internal reflection of light.