Refraction happens when a wave does what

changes direction due to a change in speed.

-got answer from wikipedia

That is according to the wave theory. The question is why....

Refraction happens when a wave enters a new medium with a different wave density.

Refraction occurs when a wave, such as light or sound, changes direction as it passes from one medium to another. This phenomenon happens because the wave's speed is different in each medium, causing it to bend or deviate from its original path.

To understand how refraction works, you can perform a simple experiment with a glass of water and a pencil. Place the glass on a table, partially fill it with water, and then place the pencil in the glass so that it appears to be bent at the water's surface. This is an example of refraction, as light waves from the pencil are bending as they pass through the water and reach our eyes.

The direction of refraction is determined by the change in the wave's speed. When a wave passes from a slower medium to a faster medium, it tends to bend away from the normal (an imaginary line perpendicular to the surface of the boundary between the two media). Conversely, when a wave passes from a faster medium to a slower medium, it bends towards the normal.

The degree of refraction also depends on the angle of incidence, which is the angle between the incident wave and the normal. Using Snell's Law, you can mathematically calculate the angle of refraction based on the indices of refraction of the two media involved.

In summary, refraction occurs when a wave passes through different mediums and changes direction due to the change in its speed.