When a wave enters a different medium with different structure - eg crystalline structure different, or a glass or a covalent molecular gas, its velocity will alter.
No waves are lost, so the frequency remains constant, but the wavelength has to alter by
v = λf.
In the animation, waves are entering a medium which causes them to slow down significantly.
In slowing and changing wavelength, the angle at which they proceed MUST change - they REFRACT.
We can calculate the way in which they refract by studying the distance the waves move in one period,T, of their motion.
Remember - we cannot lose waves, so the frequency at which they pass cannot change, so their period does not change either - f = 1 / T.
In that time, all parts of the wave move one wavelength, the longer wavelength outside the medium, the shorter wavelength inside the medium.
If the velocity in the first medium is v1, in the second, v2, then
f = v1 /λ1 = v2 /λ2 A small amount of twiddling will give λ =vT, so referring to the enlargement of the refraction animation below;
CB = distance wave in medium 1 moves in period T = v1T = λ1
OA = distance wave in medium 2 moves in period T = v2T = λ2
θi = < incidence defined against the normal = <COB
θR = <refraction defined against the normal = <OBA
Now sinθi = CB / OB , sinθR = OA / OB
It follows that sinθi / sinθR = CB / OA = v1T / v2T = v1 / v2 = n med2 rel med1
This is known as Snell's Law.
I personally find it easier to remember it as
n1 sinθ1 = n2 sinθ2
n1 and n2 are ABSOLUTE REFRACTIVE INDICES - that is, are relative to a vacuum.
TOTAL INTERNAL REFLECTION
( This has nothing to do with contemplating one's navel. )
The phenomenon occurs when a wave travels from higher to lower refractive index eg water to air for light. It is important in telecommunications - fibre optics makes extensive use of it, as does radio wave communication whereby waves are reflected off the ionosphere.
At a certain angle of incidence, the CRITICAL ANGLE, Snell's Law gives the angle of refraction as 900.
TOTAL INTERNAL REFLECTION BEGINS AT THIS POINT and for all larger incident angles.
By Snell's law,
n1 sinθc = n2 sin900 = n2, thus sinθc = n2 / n1