This phenomena is a result of the conservation of angular momentum: An isolated system's rotational intertia does not change.
Angular momentum is given mathematically by
The angle at which this takes place is called the critical angle.
We can apply Snell's law in this situation: n1 sin C1 = n2 sin (90deg)
sin C = n2 / n