The path upon which light propagates is determined by Fermat's Principle. This says that light will always travel along the path of least time. Now, we often think of the speed of light as a constant, but the constant we refer to is actually the speed of light in a vacuum. The speed of light varies considerably through different media. In fact, scientists once managed to slow light to just 38mph, by firing it through Sodium atoms cooled to almost absolute zero. The refractive index we use in Snell's law is defined as the speed of light in a vacuum divided by the speed of light in that medium. So the larger the refractive index, the greater the slowing.As a result of this slowing, the fastest path is not always the shortest. When passing through 2 materials with different refractive indices at a non-normal angle to their boundary, it is faster for the light to travel further through the material with the lower refractive index, then turn at the boundary to travel a shorter distance through the material with the higher refractive index. Think of it like using a motorway for a car journey. Sometimes it's worth going further along the motorway because it's much faster than the smaller roads, even if it actually means the distance travelled is greater.