Derivation
Classically, Fermat's principle can be considered as a mathematical consequence of Huygens' principle. Indeed, of all secondary waves (along all possible paths) the waves with the extrema (stationary) paths contribute most due to constructive interference. Supposing that light waves propagate from A to B by all possible routes ABj, unrestricted initially by rules of geometrical or physical optics. The various optical paths ABj will vary by amounts greatly in excess of one wavelength, and so the waves arriving at B will have a large range of phases and will tend to interfere destructively. But if there is a shortest route AB0, and the optical path varies smoothly through it, then a considerable number of neighboring routes close to AB0 will have optical paths differing from AB0 by second-order amounts only and will therefore interfere constructively. Waves along and close to this shortest route will thus dominate and AB0 will be the route along which the light is seen to travel.
Fermat's principle is the main principle of quantum electrodynamics where it states that any particle (e.g. a photon or an electron) propagates over all available (unobstructed) paths and the interference (sum, or superposition) of its wavefunction over all those paths (at the point of observer or detector) gives the correct probability of detection of this particle (at this point). Thus the extremal (shortest, longest or stationary) paths contribute into this interference most as they can not be completely canceled out.
In the classic mechanics of waves, Fermat's principle follows from the extremum principle of mechanics (see variational principle).
Read more about this topic: Fermat's Principle