Fermat's Principle states that light will always take the path of least travel TIME (NOT DISTANCE), at a first glance it appears that the path of least time would be a straight line from A to B, but this isn't the case. We need to bear in mind that the light travels at differing speeds depending on the medium it travels through. So by doing some fairly simple trigonometry, which I won't go into here, we can find that the path of least time is given by a relationship called Snell's law, which relates the ratio between the refractive indices of the media, and the angles of incidence and refraction. Snell's law tells us that for media with different refractive indices, the angle of incidence and angle of refraction are not equal, hence the path the light takes is not straight. This is refraction.