Dear Student ,
When a light is reflected from a more dense medium then crests get reflected as troughs and troughs as crests i.e. the reflected wave undergoes a 180° phase change (or we can say out of phase) on reflection.
In reflection from a denser medium the ray of light suffer a phase difference of 180o. We know that waves(light rays ) also have energy and momentum and when they encounter boundaries (either hard, like a wall, or soft), something has to happen to the incident energy and momentum. To see what happens to waves reflecting from a more dense medium (a hard boundary), let's imagine a wave produced on a string that's fixed to an end. Now let a upward pulse (crest) travel to the right at fixed end. As the wave travels through the length of the string towards the fixed end, the forces on the string are upward at the forefront of the wave (this is what allows the wave to move along). When the pulse reaches the fixed end , the wave transfers these upward forces to the wall. And, by Newton's third law, the wall pushes back on the string with an equal force but in the opposite direction. This reaction force creates a new wave which propagates backward (this reflected wave travels from left to right, trough) with the same wavelength and amplitude but with the opposite polarity i.e. suffer a change of phase 180o.
Regards