Thermodynamic and stochastic theory of nonequilibrium systems: A Lagrangian approach to fluctuations and relation to excess work