This is a general property of waves. If you have waves reflecting off a clamped point (like waves running on a string that you pinch hard at one point), the waves get phase inverted. The reason is the principle of superposition and the condition that the amplitude at the clamped point is zero. The sum of the reflected and transmitted wave must be the amplitude of oscillation at all points, so that the reflected wave must be phase inverted to cancel the incoming wave.
This property is continuous with the behavior of waves going from a less massive string to a more massive string. The reflection in this case has opposite phase, because the more massive string doesn't respond as quickly to the tension force, and the amplitude of oscillation at the contact point is less than the amplitude of the incoming wave. This means (by superposition) that the reflected wave must cancel part of the incoming wave, and it is phase reflected.
When a wave goes from a more massive string to a less massive string, the less massive string responds with less force, so that the derivative at the oscillating end is flatter than it should be. This means that the reflected wave is reflected in phase with the incoming wave, so that the spatial derivative of the wave is cancelled, not the amplitude reduced.
In optical materials of high density are analogous to strings with a higher density, hence the name. If you go into a material with low speed of light, the time derivative term in the wave-equation is suppressed, so that the field responds more sluggishly, the same way that a massive material responds more sluggishly to tension pulls. Since the eletric field response in these materials is reduced, the reflected wave is phase inverted to make the sum on the surface less, as is appropriate to match with the transmitted wave.