The bending of light, or refraction, occurs when light passes between two mediums with different refractive indexes. The change in media causes the light to change speed and wavelength, and thus the light appears to bend.
The refractive index of a medium measures how quickly light is able to pass through it. When light encounters a medium with a differing refractive index, it appears to bend as a result of the change of speed. To illustrate why this happens, imagine a line of people walking forward at a steady rate toward a non-perpendicular line marked on the floor. When crossing this line, each person is to slow his pace. While the pace of every person crossing the mark is slowed, the pace of the line behind them remains the same, and so the direction of the line becomes altered at this boundary.
To use a common example, this explains why light traveling through air (a medium it can pass through quickly) into a prism (a medium that slows the speed of light) appears to bend. The exception to this rule occurs when light hits the boundary of the second medium at a perfectly perpendicular angle, in which case no bending occurs.