Why does a ray of light bend when it travels from one medium into another?

Why does a ray of light bend when it travels from one medium into another?

The ray of light will follow the path after refraction as below:

image