Why does a prism refract light into a rainbow? Why does a prism refract light such that the different frequencies of light “spread out”? The same goes for rainbows, why do the raindrops “spread out” the different frequencies of light? 
 A: This phenomenon is due to the simple concept of refraction. Due to the difference in the wavelengths of each component of white light, this dispersion of white light occurs in denser/viscous media.
Snell's law states that, $$sin(i)/sin(r) = n2/n1 = v1/v2$$ where i is the angle of incidence and r is the angle of refraction, n1 and n2 are the refractive indices of media 1 and 2 respectively.
We also know that $v = f*y$, where f is the frequency of light and y is the wavelength. Hence, the angle of deviation depends on wavelength.
This is also the reason for the "spreading out" of light by a raindrop thus leading to the formation of rainbow.

In the image given above, the red and violet components of white light have different wavelengths and hence the angle of refraction differs significantly in the viscous media.
A: As the other answer gives you the formula, and the reasoning, the real reason is that different wavelength photons diffract at different angles at the edge of two different medium.
Now the question remains, why do different wavelength photons change angle differently at the edge of a new medium?
I am going to use lattice structure here, but for air and water it is hard to talk about a lattice structure, so in this case what I mean is the molecular structure.
First of all, lets clarify that the change in angle not only happens at the edge of a medium, but:


*

*the angle of the photons will change even in the same medium, at the edge of different lattice structures, or different densities

*even if you cut that triangular glass prism into two pieces, and put them exactly back together as they were before, there will be diffraction, as the lattice structure is irreversibly broken at the edge of the two pieces
So, shooting back the question as a boomerang, why do photons travel parallel in white light (inside a single medium, with no density and lattice structure changes):


*

*because the medium has a continuous, unbroken lattice structure, no density changes

*this lattice structure is uniform (same structure, same atoms, same molecules, same covalent bonds) throughout the whole path of the photons  
So, everytime the lattice structure changes in any way (like density or broken or the structure itself, or different covalent bonds), the photons will diffract (change angle), and the different wavelength photons will change angle in different amounts.
Now you see that the answer to your question, why do the photons change angle in different amounts still remains, and the answer is the lattice structure. To be more precise, in air, or water, there is no lattice (like a solid), but the structure of the molecules, and densities, can still be different. Still, water can act as a prism in air, and you get the same rainbow effect. Why? Because the edge of the water and air has a different molecular structure. Now the question is why do different wavelength photons bend differently at the edge of different structural media? 
So shooting back the question again as a boomerang, why does a group of certain (same) wavelength photons bend parallel (the same way) at the edge of a new medium? Why does the wavelength decide the angle? First of all, there is no two photons with the same exact wavelength, as the wavelength is continuous. But, with our eyes, we cannot tell the difference at a certain level, so let's say there are two photons that seem to have the same wavelength (for our viewing purposes). Why do these photons bend at the same angle at the edge? Why do you see a static image of the colors at the prism, and why doesn't the absolute angles of the different color photons randomly change? And why doesn't the relative angle randomly change?
The answer could be classical or QM:


*

*classical answer is that the different wavelength photons travel at different speeds inside a denser medium.

*the QM answer is that the photons building up the EM wave interact with the lattice structure, and this elastic scattering decides the angle.
Let me elaborate on the QM answer. When a photon interacts with an atom, three things can happen:


*

*elastic scattering, the photon keeps its phase, energy, and changes angle

*inelastic scattering, the photon keeps part of its energy and changes angle

*absorption, the photon gives all its energy to the atom
Now in the case of the prism, it is elastic scattering. This is the only way, that the energy of the photons is kept, phase is kept, and relative energy is kept, and relative phase is kept.
Now with elastic scattering, as the photons in the white light, combined of all the different wavelengths from the Sun traveling parallel reach the edge of the glass prism from the air, what happens is:


*

*the different wavelength photons start interacting with the lattice structure of the glass prism. The different wavelength photons elastic scattering will produce a certain angle change for the photons

*photons with the same wavelength will interact similarly with the lattice structure, and create the same angle, these photons (same wavelength) will continue in one separate beam separated from the other beams (other wavelengths)
Now this is not the complete reality. Imagine the double slit experiment. The photon (shot one at a time) will have its partial waves pass through both slits and interfere with each other, sometimes constructively and sometime destructively. The destructive interference will not be visible (just its void, missing dot), creating a dark area. The constructive interference will create a visible bright area.
In reality with the prism, the photons interact with the lattice structure of the glass, and the partial waves create interference. Some of the interference will be destructive, basically, in all directions where you do not see the certain beam (certain wavelength photons). In only one angle, direction, will be the constructive interference, where you will see the certain wavelength photons in one beam.
So the answer to your question why different angles for different wavelength is that the elastic scattering as an interaction between the photons and the lattice atoms will create an angle that is:


*

*mostly similar for similar wavelength photons

*different for different wavelength photons
 
It is all about probabilities at the QM level, but if you use a lot of photons, that build up the white light, most of the similar wavelengths will change angle in the same amount, and they will be visibly separated from the other wavelengths.
