What exactly happens when total internal reflection takes place at a quantum level? What happens when light interacts with the boundary between 2 mediums at a quantum level? Why is it totally reflected back when it is travelling from an optically denser to a less dense medium? How does the quantum of light know that its going through such a boundary?
 A: 
"How does the quantum of light know that its going through such a boundary?"

By obeying the quantum mechanical solution of the boundary value problem "photon scattering off boundary"
Here is the light phenomenon:

Total internal reflection is a phenomenon which occurs when a propagating wave strikes a medium boundary at an angle larger than a particular critical angle with respect to the normal to the surface. If the refractive index is lower on the other side of the boundary and the incident angle is greater than the critical angle, the wave cannot pass through and is entirely reflected.

The refractive index is a collective measure of the atoms/molecules of the lattice on which the light is incident and depends on the collective fields built up by them. This is the field on which each individual photon will be scattering and partakes of the boundary conditions of the problem.The fields must be such that the individual photons from that angle on  are scattered in only a backward direction .
