Rainbows form when sunlight enters water droplets, reflects inside, and exits. Different colors bend by different amounts - red bends least, violet most. This separates white light into colors.
That's why you only see rainbows when the sun is behind you and it's raining in front.