In short
Total internal reflection happens when light tries to leave a denser medium for a rarer one at an angle larger than the critical angle, and all of it is reflected back inside.
Two things must be true. The light must travel from a denser medium to a rarer one, for example glass to air. And its angle of incidence must be larger than the critical angle , where . Optical fibres, diamonds' sparkle and a mirage all depend on this effect.
For glass of refractive index in air, , so . A ray inside the glass meeting the surface at is totally reflected.