You've all seen rainbows on those days where it's raining and the sun's out at the same time. But what about those times when you see a rainbow-like halo around the sun or moon?
It's the same physics, really. The halos (or, sometimes known as "sundogs" around the sun) are usually seen when there are high cirrus clouds overhead.
Those clouds are made of tiny ice crystals, which will refract the sunlight much like a prism will and voila! You have a rainbow halo around the sun.
Sometimes the rainbow isn't in the form of a halo, but just colors that just streak across the clouds. Fire rainbows, or more officially (and more boringly) known as "circumhorizonal arcs" are also caused by ice crystals in the thin, distant clouds being at just the correct angle to refract the sunlight into the colors of the prism.