When light travels from one medium to another, the light gets bent and this phenomenon is called refraction of light. The bending of light is due to the different speeds of light in different media having different refractive indexes.
And by doing so light minimizes the time taken to reach a point from the first medium to a point in the second medium.