The bending of light ray when travelling from one medium to other is called as Refraction. This is based on Fermat's principle. Which means light ray always travels between two points in a path which need the shortest possible time to cover.

This Is a Certified Answer

Certified answers contain reliable, trustworthy information vouched for by a hand-picked team of experts. Brainly has millions of high quality answers, all of them carefully moderated by our most trusted community members, but certified answers are the finest of the finest.
The actual reason for the bending of light at the boundary of two transparent media is explained by the wave theory of light. The light propagates as spherical wave fronts in space.  At a very small scale (in the order of wave lengths of light), the wave front can be considered as a plane wave front.

Visible Light is an electromagnetic wave and has different speeds in different media.  The light incident perpendicular to the boundary travels straight.  But light waves incident at angle bend because the spherical wave front travels at a different speed in the second medium from the first.

This explanation also finds and derives that
   refractive index of the second medium with respect to the first
      = Sine of incident angle / Sine of refraction angle
      = speed of light in the first medium / speed of light in second medium

The is also stated in another form as follows:  Light waves bend in order to minimize the time taken to reach a point from the first medium to a point in the second medium.