The change in direction comes about as a requirement to satisfy boundary conditions to maxwell's equations. If the velocity of light in the two media is different then the direction will change at the boundary unless the angle of incidence is normal to the surface.
The deflection is given by Snell's law, but the reason is that the tangential electric field and the normal flux density must both be continuous across the boundary in order to satisfy maxwell's equations.
It also comes from the principal of "Stationary Action" from which Snell's Law is easily derived.