Although light can be thought of as individual photons, ie. particles of light, it also has wave properties. Refraction is a consequence of the wave nature of light. When light travels from one medium to another, its speed changes because it interacts with each medium differently since the atoms in each medium respond to the electric and magnetic fields of the light waves in specific ways that are determined by the atomic structure of the atoms in each material and the molecular structure of how those atoms are arranged.
So let's say that light is traveling from air to water. Since water interacts with light in such a way as to slow it down more than air, when the light wave moves between the air and water, its speed suddenly changes. Since the speed of any wave is its frequency times its wavelength, that means that one of those factors has to change in order for the speed of the wave to change. In the case of light, its frequency and therefore color, is set when it was emitted, but its wavelength can and does change (this happens in such a way that the sudden change in speed from one medium to another is matched by a proportional change in wavelength, otherwise the wave would experience a discontinuity... essentially a kink in the wave produced at the interface between the two media). The CHANGE in wavelength from on medium to another is greater for higher frequencies of light, therefore blue light refracts more than red and the rainbow has the pattern it does. This is why refractive index is wavelength/frequency dependent.
But as to why light bends rather than just continuing straight on when it slows down, I don't know. Someone please help me with that one.