When sunlight hits a rain droplet, some of the light is reflected. The electromagnetic spectrum is made of light with many different wavelengths, and each is reflected at a different angle. Thus the spectrum is separated, producing a rainbow.