
(the following answer is included essentially in "The Feynman LECTURES ON PHYSICS-Mechanics, Radiation & Heat ,Vol. 1, 26-3 Fermat's principle of least time.)
Suppose you are at point A in the land and a screaming girl is at point B in the sea. You can run with a speed $\:v_{1}\:$ on the land greater than the speed $\:v_{2}\:$ you can swim in the sea. At a moment you decide to follow the path ACB spending time $\: t_{1}=AC/v_{1}\:$ running on the land and time $\: t_{2}=CB/ v_{2}\:$ swimming in the sea, that is total time
\begin{equation}
t_{tot}=t_{1}+t_{2}=\dfrac{AC}{v_{1}}+\dfrac{CB}{v_{2}}
\tag{01}
\end{equation}
But after a while you change your mind and decide to displace the point C on shoreline a little to the right to the point D. But then you are wondering if by such a displacement you shorten the total time or not.
For infinitesimally small displacement $\:CD\equiv \Delta x\:$ you can do the following approximations :
\begin{equation}
AE \approx AC \qquad \theta_{1}^{\prime} \approx \theta_{1} \qquad BZ \approx BD \qquad \theta_{2}^{\prime} \approx \theta_{2}
\tag{02}
\end{equation}
You realize that on one hand you decrease the swimming distance by
$$
BC-BD \approx CZ=\Delta x \cdot \sin\theta_{2}
\tag{03}
$$
so changing (decreasing) the swimming time by
$$\Delta t_{2}=t_{2}^{\prime}-t_{2}=-\Delta x \cdot \dfrac{\sin\theta_{2}}{v_{2}}
\tag{04}
$$
On the other hand you increase the running distance by
$$
AD-AC \approx DE=\Delta x \cdot \sin\theta_{1}
\tag{05}
$$
so changing (increasing) the running time by
$$\Delta t_{1}=t_{1}^{\prime}-t_{1}=+\Delta x \cdot \dfrac{\sin\theta_{1}}{v_{1}}
\tag{06}
$$
So, balancing, the total time change is
$$
\Delta t_{tot}=t_{tot}^{\prime}-t_{tot}=\left(t_{2}^{\prime}+t_{1}^{\prime} \right)-\left(t_{2}+t_{1}\right)=\Delta t_{2}+\Delta t_{1}= \Delta x \cdot\left(\dfrac{\sin\theta_{1}}{v_{1}}- \dfrac{\sin\theta_{2}}{v_{2}}\right)
\tag{07}
$$
This means that if
$$
\left(\dfrac{\sin\theta_{1}}{v_{1}}- \dfrac{\sin\theta_{2}}{v_{2}}\right) > 0
\tag{08}
$$
then moving infinitesimally to the right, $\: \Delta x >0 \:$, we increase the time while moving to the left, $\: \Delta x <0 \:$, we decrease the time.
So in case that the condition (08) is valid then in order to find a shorter time we must search to the left of point C.
If
$$
\left(\dfrac{\sin\theta_{1}}{v_{1}}- \dfrac{\sin\theta_{2}}{v_{2}}\right) < 0
\tag{09}
$$
then moving infinitesimally to the right, $\: \Delta x >0 \:$, we decrease the time while moving to the left, $\: \Delta x <0 \:$, we increase the time.
So in case that the condition (09) is valid then in order to find a shorter time we must search to the right of point C.
But finally, if
$$
\left(\dfrac{\sin\theta_{1}}{v_{1}}- \dfrac{\sin\theta_{2}}{v_{2}}\right) = 0
\tag{10}
$$
then either moving to the right,$\: \Delta x >0 \:$, or moving to the left , $\: \Delta x <0 \:$, the change is infinitesimally zero. This is the definition of the extreme points of a function.
So, condition (10) is the one of the shortest time and if you are a light ray then in terms of refraction indices
$$
v_{1}=c_{1}=\dfrac{c_{0}}{n_{1}}, \quad v_{2}=c_{2}=\dfrac{c_{0}}{n_{2}}
\tag{11}
$$
and (10) is Snell's Law
$$
\bbox[#FFFF88,12px]{n_{1}\sin\theta_{1}=n_{2}\sin\theta_{2} \qquad \textbf{(Snell's Law)}}
\tag{12}
$$
EDIT


Does this mean that the path followed by light in travelling through different media will give a straight line if different media are transformed into like first media?
– cheesebread May 21 '16 at 12:29