Nowadays this function is frequently called "Hamilton's action", though this is not a good idea because it causes confusions with what Hamilton, like all his predecessors, called "action", which is the integral of the kinetic energy.
The "Principal Function S", which is a scalar value, i.e. a relativistic invariant quantity, is the line integral of the Lagrangian over the trajectory in space-time, i.e. it is the line integral of the energy-momentum 4-vector over the trajectory in space-time.
Like any line integral of a vector, the line integral of the energy-momentum 4-vector is equal to the line integral over the trajectory of its projection on that trajectory.
This is why the Lagrangian is the projection of the energy-momentum 4-vector. Hamilton has found the correct form of this line integral in relativistic theory, even if that was about 3 quarters of century before the concept of 4-vectors became understood.
The "Principal Function S", i.e. the integral of the energy-momentum, can be considered as a more fundamental quantity than the Lagrangian, which is its derivative (the energy-momentum vector is its gradient). In quantum mechanics the "Principal Function S" is the phase of the wave function, so it is even more obvious that it must be an invariant quantity.
A resource I created:
Calculus of Variations as applied in physics: http://cleonis.nl/physics/phys256/calculus_variations.php
Hamilton's stationary action: http://cleonis.nl/physics/phys256/energy_position_equation.p...
In that resource I show why it works.
In an earlier answer I gave more information about that resource. To find that earlier answer: go up to the entire thread, and search on the page for my nick: Cleonis
If anyone has a reference/book/paper that allows you to learn this concept more intuitively, I'd be grateful.
It is possible to go in all forward steps from F=ma to Hamilton's stationary action; that is what I present.
The path from F=ma to Hamilton's stationary action consists of two stages: (1) Derivation of the work-energy theorem from F=ma (2) Demonstration: when the conditions are such that the work-energy theorem holds good then Hamilton's stationary action will hold good also.
I recommend that you first absorb the presentation of the subset of Calculus of Variations that is applied in physics: http://cleonis.nl/physics/phys256/calculus_variations.php
Discussion of Hamilton's stationary action: http://cleonis.nl/physics/phys256/energy_position_equation.p...
These presentations are illustrated with interactive diagrams. Each diagram has one or more sliders for manipulation of the contents of the diagram. That way a single diagram can offer a range of cases/possibilities.
About my approach: I think of Hamilton's stationary action as an engine with moving parts. To show how an engine works: construct a model out of translucent plastic, so that the student can see all the way inside, and see how all of the moving parts interconnect. My presentation is in that spirit.