What is the proper way to understand Fermat's principle? I am studying Fermat's principle for the first time and the basic knowledge that I could gather said that it states that "that the path taken by a ray between two given points is the path that can be traversed in the least time".
When we see ray diagrams in Optics, we always see that:

*

*1. From Rarer medium to Denser medium:
Speed of light is more in rarer medium and lesser in a denser medium so in order to travel more distance in the denser medium, the ray bends towards the normal to take the path that can be traversed in the least time.

I understood this part but coming to the second part.

*

*2. From Denser medium to rarer medium:
Speed of light is more in rarer medium and lesser in the denser medium.
Now here my question arises.
In ray diagrams, we see that if the light is going from denser medium to rarer medium, it bends away from normal. But why? If Fermat's principle is followed, then light should still bend towards normal as then, the path it takes could be traversed in lesser time.
I believe I am missing a crucial point of Fermat's principle, it would be highly appreciated if someone could resolve my query.
 A: The following may be useful.  The key thing to keep in mind is that you need to also include the "two points" that the ray is traveling between.  If you set up the problem including these two points, one inside the denser medium and one inside the rarer medium, then the argument follows.
I hope this helps.
A: Fermat's principle is one way of rationalizing the bending of a light ray (or other waves like sound or seismic waves that have well-defined wave fronts) travelling from point A to point B when traversing across a boundary between regions of two different indices of refraction.  As you correctly summarized in the first part of your post, the light ray bends towards the normal when moving from a region of faster speed to a region of slower speed.
In response to the specifics of your question, if you imagine the light ray moving in the reverse direction (from point B to point A) with the light moving from a region of slower speed to faster speed, you would see the light bending the reverse way as it retraces the path it took , that is away the normal.  This symmetry is evident in Snell's Law
$$n_{1} \sin (\theta _{\rm incidence }) = n_{2} \sin (\theta _{\rm refraction })$$
as the equation applies for both path directions.
It is important to recognize that there are two different types of minimum paths (minimum distance and minimum transit time).  The shortest path by distance is a straight line, but the shortest path by time is the bent path, which is what waves follow.  As an equation:
$$time_{transit} = \frac{distance{_{1}}}{velocity _{1}} + \frac{distance{_{2}}}{velocity _{2}}$$
has a minimum value along a bent path.
This has also been explored by a math professor who would walk his dog along the beach and throw a tennis ball into the water.  The dog ran at a faster speed on sand than in the water.  The professor observed that his dog took a bent path that was close to the calculated minimum path predicted by Snell's Law, rather than chasing after the ball along the path of minimum distance.

*

*T. J. Pennings, "Do Dogs Know Calculus?", Coll. Math. J. 34:3, 178-182 (2003).

I worked out this problem some time ago and am including a diagram of the main analysis.  The first image shows the calculation of the time of transit for the straight path, with speed of the dog on sand (5 m/s) > speed of dog in water (0.5 m/s).  The dog is 4 m from the water initially, and the ball is 12 m away from the sand.  I made these numbers up.

The second images shows the calculation of how far the dog should run on the sand before moving into the water to take the path of minimum transit time.

The third image shows the calculation of minimum travel time from the geometry calculated in the second figure.

I also include a screen capture of a spreadsheet analysis where I verified that the path calculated has the minimum transit time among many path alternatives that vary among distance d along the sand before entering the water:

There is a completely independent way of deriving Snell's Law from wave fronts that I have illustrated in a diagram for another question:
Can a light wave front narrow or widen in refraction, and what does this really mean for light?
