A rainbow is produced by the dispersion of sunlight by the raindrops in the atmosphere.
Rainbow is formed when the light rays travel through the water droplets that are present in the atmosphere. As the light rays travel through the water droplets, there are two processes taking place and they are reflection and refraction.
When the light ray is reflected off from a water droplet, it bounces back in the opposite direction from where it originated. The light ray travels in a different direction when refraction takes place. Therefore, a rainbow is a result of bending white light in different directions. Since the white light is getting separated inside the water droplet, the refracted light appears as separated colours to the human eye.