Why does light change direction when it travels through glass? This was explained to me many years ago, by a physics teacher, with the following analogy:
"If someone on the beach wants to reach someone else that is in the water, they will try to travel as much as they can on the beach and as little as possible on the water, because this way they will get there faster."
I'm paraphrasing of course, but this is as accurate as I recall it.
This explanation makes no sense to me. Was he telling me the light knows where it is going? It wants to get there faster? It chooses a different direction?
(No need to answer these questions, this was just me trying to understand the analogy.)
My attempts to clarify the issue were without success and many years later I still don't know.  
Why does light change direction when it travels through glass?
 A: The teacher was trying to explain Fermat principle which is one of the simplest variatinonal (least action) principles of classical physics. And your question seems to express to a  common frustration over the seemingly "teleological" property of all variational principles: how does the (partilce, wave, ray ...) "know" in advance which path to follow? Wikipedia article on the principle of least action specifically addresses this frustration (but does not give valid references, unfortunately).
The laws of propagation are local in time and space but it is sometimes easier to deduce their outcomes using non-local mathmatical constructions such as a Fermat principle. The teacher gave a great analogy to explain the principle but should not expect toget a step-by-step picture out of it.
A: The explanation is very simple! The reason light changes direction ("bends") when traveling through glass, is because light travels slower in glass than in air. If now, you also want to know why light travels slower in glass than air, it is because the density of glass is higher than air and the electromagnetic fields of the glass molecules interfere more, than the air molecules, with the propagation of light.  
As you can see, no cognitive powers need be given, to light!
In general, light will "bend" any time it goes from one medium to another medium with higher or lower density.  It is really the difference in densities that causes the bending of the light.   
A: Your teacher's explanation, as you describe it, seems a little disengenuous. It is true that light goes from A to B in the shortest possible time, given different speeds in different media. From a human perspective, it almost seems like the light rays are calculating all their possible path options and picking the most optimal. The real question is, what is the physical mechanism that allows light to perform what appears to be the act of a rational being with infinite powers of calculation?
The answers to this question lie entirely within the domain of classical wave theory. There is no further insight to be gained by looking into the mysteries of quantum mechanics. Primeczar gives the bare outline of an explanation wherein the polarized charges within the glass are taken as the source of new fields. It's actually simpler to do the calculation if you simply accept that the propagation speed in glass is lower than air, and focus on the excess polarization that appears only on the surface of the glass, not within the body. It is then unnecessary to determine the amount of polarization, only to recognize that it must have some kind of constant phase relationship with the driving force of the incoming wave. The angle of refraction follows unambigously from this purely geometrical argument.
A: Electron & atomic beams also exhibit refraction.Deflection angle is due to the resistance to motion which depends on particle's size and medium's density.
Photon behaves as particle in this effect.If it comes straight in,resistance pushes straight back and doesn't change direction.They call straight in or perpendicular to the surface "normal" in the Wikipedia article.
If it comes in at an angle,the vector of resistance force is analyzed in two components: one parallel and one vertical to ("normal").The last component  pushes the photon away from the "normal" direction.
A: Have a look at the index of refraction, which is the ratio of the velocity of light in  vacuum  to the one in the medium. Light travels more slowly in a medium and thus "glances off" and changes direction, as in the drawing.

$n$ = speed of light in a vacuum / speed of light in medium
For example, the refractive index of water is 1.33, meaning that in a vacuum, light travels 1.33 times as fast as it does in water.

I should add that refraction is observed in all types of waves,

Refraction is described by Snell's law, which states that the angle of incidence $\theta_1$ is related to the angle of refraction $\theta_2$ by
$\frac{\sin(\theta_1)}{\sin(\theta_2)}= \frac{v_1}{v_2}=\frac{n_2}{n_1}$
where $v_1$ and $v_2$ are the wave velocities in the respective media, and $n_1$ and $n_2$ the refractive indices. In general, the incident wave is partially refracted and partially reflected

A: Light is an electromagnetic wave. electric field and magnetic field oscillations are perpendicular to each other. They keep on generating each other. The direction of propagation of an EM wave  is given by the Poynting Vector which is a vector in a direction perpendicular to both the directions of vibration of electric field as well as magnetic field. Now when light goes from one medium to another, say, from air to glass, what happens is that the E- field polarizes the atoms of glass. Now as the E field is oscillating the polarizing effect is also oscilating , that is , it changes direction of induced dipole moments. so The positive and negative charges inside the glass atoms now also oscillate and generate their own electric and magnetic field. so the net electric and magnetic field at any instant will be the resultant of the fields produced by the original source as well as the fields produced by the charges inside atoms of glass.Hence as the directions of E- field and B- field change ,the corresponding poynting vector also changes. Hence the direction of light is changed. 
A: Light changes direction when passing through a prism because the density of air is different to the density of glass. Therefore the speed changes, when something (i.e. glass or a prism) is optically dense it is harder for light to travel through it, thus making it's speed decrease. When the waves meet the prism they slow down, so if they meet it at an angle part of the first part of the wave slows down first, leaving the remaining part to slow down later. This is what changes the direction and makes the light travel in a different way.
