How does light speed up after coming out of a glass slab? As I learned today in school, my teacher told me that when light enters a glass slab it slows down due to the change in density and it speeds up as it goes out of the glass slab. This causes a lateral shift and the light emerges out from the point different than that from where it should have actually emerged from.
Okay so what I mean to ask is, when light enters point A on glass slab and emerges from point C why does the light speed up? Where does it get the energy it has lost when it entered the glass slab?
P.S.: Also, if I place a very very very large glass slab and make a beam of light pass through it will the light never come out as all the energy was lost in place of heat?
 A: When light is propagating in glass or other medium, it isn't really true, pure light. It is what (you'll learn about this later) we call a quantum superposition of excited matter states and pure photons, and the latter always move at the speed of light $c$.
You can think, for a rough mind picture, of light propagating through a medium as somewhat like a game of Chinese Whispers. A photon is absorbed by one of the dielectric molecules, so, for a fantastically fleeting moment, it is gone. The absorbing molecule lingers for of the order of $10^{-15}{\rm s}$ in its excited state, then emits a new photon.  The new photon travels a short distance before being absorbed and re-emitted again, and so the cycle repeats. Each cycle is lossless: the emitted photon has precisely the same energy, momentum and phase as the absorbed one. Unless the material is birefringent, angular momentum is perfectly conserved too. For birefringent mediums, the photon stream exerts a small torque on the medium. 
Free photons always travel at $c$, never at any other speed. It is the fact that the energy spends a short time each cycle absorbed, and thus effectively still, that makes the process have a net velocity less than $c$. 
So the photon, on leaving the medium, isn't so much being accelerated but replaced.

Answer to a Comment Question:


But how the ray of light maintain its direction? After it is absorbed by first atom, how does it later knows where to shot new photon again? Where is this information is preserved?

A very good question. This happens by conservation of momentum. The interaction is so short that the absorber interacts with nothing else, so the emitted photon must bear the same momentum as the incident one. Also take heed that we're NOT a full absorption in the sense of forcing a transition between bound states of the atom (which gives the sharp spectral notches typical of the phenomenon), which is what David Richerby is talking about. It is a transition between virtual states - the kind of thing that enables two-photon absorption, for example - and these can be essentially anywhere, not at the strict, bound state levels. As I said, this is a rough analogy: it originated with Richard Feynman and is the best I can do for a high school student who likely has not dealt with quantum superposition before. The absorption and free propagation happen in quantum superposition, not strictly in sequence, so information is not being lost and when you write down the superposition of free photon states and excited matter states, you get something equivalent to Maxwell's equations (in the sense I describe in my answer here or here) and the phase and group velocities naturally drop out of these.
Another way of qualitatively saying my last sentence is that the absorber can indeed emit in any direction, but because the whole lot is in superposition, the amplitude for this to happen in superposition with free photons is very small unless the emission direction closely matches the free photon direction, because the phases of amplitudes the two processes only interfere constructively when they are near to in-phase, i.e. the emission is in the same direction as the incoming light. 
All this is to be contrasted with fluorescence, where the absorption lasts much longer, and both momentum and energy is transferred to the medium, so there is a distribution of propagation directions and the wavelength is shifted.

Another comment:

There was a book which said mass of photon increases when it enters glass... I think that book was badly misleading.

If you are careful, the book's comment may have some validity. We're talking about a superposition of photon and excited matter states when the light is propagating in the slab, and this superposition can indeed be construed to have a nonzero rest mass, because it propagates at less than $c$. The free photons themselves always propagate at $c$ and always have zero rest mass. You actually touch on something quite controversial: these ideas lead into the unresolved Abraham-Minkowsky Controversy.
A: Your two questions are based on the erroneous notion that light looses energy in going through a glass slab. Light has a propagation speed which depends on the density of the medium.  When a light beam goes from vacuum (air) into glass, the only thing that happens is that the wave gets delayed (takes more time to travel the same distance, because of the higher density).  Since v = d/t, if t gets bigger v gets smaller (for same d).  What this means is that the propagation speed of light in glass gets slowed down.  Once it passes the glass, the delay is gone so light resumes its previous propagation speed in air.
Although there is a small loss due to some photons striking the "cores" of the glass molecules, the major loss is due to the light "spreading out" (not coherent light).  With the proper light (coherent), one can use "miles" of glass fibers and still have light come out the far end.      
A: A classical explanation to supplement Rod's excellent quantum mechanical one:
If you make a Huygens construction of wave propagation (I assume you know how to do that) then every point on the wave front is treated as the source of a new wave of the same frequency and phase. How that wave propagates depends on the medium it encounters. So the Huygens wavelets generated at the exit face of the glass, which "see" only empty space in front of them, simply propagate with the velocity that is appropriate for them - just like the wavelets at the entrance face see a medium of higher refractive index and thus slower propagation (and refraction for non-normal incidence) so the ones at the exit face see the opposite.
As for "heat loss" - if there are loss mechanisms inside the glass photons will get absorbed but there is always a (very small) probability that a photon will make it through - in practice that probability can become so small that you can assume no light will be detected but that is not the same thing as saying "no light can make it through this slab". Likelihood vs. certainty.
