The question of whether/when a system of differential equations describing a law of motion has a Lagrangian formulation is determined by the Helmholtz Conditions.
The Inverse Problem For Lagrangian Mechanics
https://en.wikipedia.org/wiki/Inverse_problem_for_Lagrangian_mechanics
The question of what the Lagrangian represents is that it is a way to generate set of relations between the variables that govern dynamics with the variables that govern kinematics, which may be termed "constitutive relations". Lagrangians generate constitutive relations.
The Lagrangian is one implementation of an underlying geometry, called a "symplectic" geometry that connects kinematic variables with their conjugate dynamic variables, in the description of dynamics and laws of motion. The Hamiltonian is another.
The realization that such a deep-seated infrastructure, related to symplectic geometry, underlies many or most of the fundamental theories in physics was relatively slow in the coming, but it is all-pervasive. It wasn't any kind of "ah ha!" moment, but just a realization that slowly creeped up on everyone over the years.
It is an infrastructure that is shared in both classical physics and quantum physics and is a feature centrally involved in the bridge that connects the two.
If $q^1, q^2, q^3, ...$ are the kinematic variables entering the description of a system, then $p_1, p_2, p_3, ...$ are the corresponding conjugate dynamic variables. The archetypal example is the position of a body $\left(q^1, q^2, q^3\right) = (x, y, z)$ and its corresponding momentum $\left(p_1, p_2, p_3\right) = \left(p_x, p_y, p_z\right)$.
Two sets of laws are satisfied by these quantities - the "kinematic laws" that connect the components of the corresponding velocities $\left(v^1, v^2, v^3, ...\right)$:
$$v^a = \frac{dq^a}{dt} \hspace 1em (a = 1, 2, 3, ...),$$
and the "dynamic laws" that connect the components of the corresponding forces $\left(f_1, f_2, f_3, ...\right)$:
$$f_a = \frac{dp_a}{dt} \hspace 1em (a = 1, 2, 3, ...).$$
The Lagrangian is one way to connect the two sets of quantities; a connection which could be considered as "constitutive relations" that determine the structure of the system being described by the dynamics. A typical example are the relations used in dynamics for gravitational motion:
$$\left(p_1, p_2, p_3\right) = m \left(v^1, v^2, v^3\right), \hspace 1em
\left(f_1, f_2, f_3\right) = -\frac{GM}{{q^1}^2 + {q^2}^2 + {q^3}^2} \frac{\left(q^1, q^2, q^3\right)}{\sqrt{{q^1}^2 + {q^2}^2 + {q^3}^2}}.$$
Although the example illustrates the dynamics for just one body, you actually throw in all the coordinates of all the parts that describe the overall system - meaning: all the coordinates of all the bodies that make up the system and whatever other attributes describe the system.
What was gradually discovered is at the center of the underlying infrastructure is a basic object that is cast in the language of differential forms and is called the "symplectic two-form":
$$ω = dp_1 ∧ dq^1 + dp_2 ∧ dq^2 + dp_3 ∧ dq^3 + ...$$
As a point of note, differential forms follow a Grassmann algebra, which means (for instance) $da ∧ db = -db ∧ da$, for odd-degree differential forms. Each of the $dp_a$ and $dq^a$ are degree 1, while wedge products add degrees, so $ω$ and its individual terms are each degree 2. Degree 0 differential forms are just ordinary scalars.
Differential forms are also the native language of symplectic geometry.
The key statement involving $ω$ is that it possesses symmetries with respect to certain types of geometric transforms or other transforms. This implements a "relativity" principle for the dynamics.
Chief amongst these is that the dynamics should be symmetric with respect to translation in time. This is implemented by taking the time derivative of $ω$ and setting it to 0. The actual operation is called a "Lie derivative" and would look like this:
$$\begin{align}
_{d/dt} ω &= _{d/dt} \left(\sum_a dp_a ∧ dq^a\right) \\
&= \sum_a \left(_{d/dt} \left(dp_a\right) ∧ dq^a + dp_a ∧ _{d/dt} \left(dq^a\right)\right) \\
&= \sum_a \left(d\left(_{d/dt} p_a\right) ∧ dq^a + dp_a ∧ d\left(_{d/dt} q^a\right)\right) \\
&= \sum_a \left(d\left(\frac{dp_a}{dt}\right) ∧ dq^a + dp_a ∧ d\left(\frac{dq^a}{dt}\right)\right) \\
&= \sum_a \left(df_a ∧ dq^a + dp_a ∧ dv^a\right). \\
\end{align}$$
Setting this to zero results in:
$$
\sum_a \left(df_a ∧ dq^a + dp_a ∧ dv^a\right) = 0
$$
and out of this arises the general form for the constitutive laws that connect the dynamic and kinematic variables to one another; the cloth out of which all the formulations - be they Hamiltonian, Lagrangian or otherwise - are cut.
The different formulations are different ways to integrate this equation. One way, used for Hamiltonians is:
$$\begin{align}
0 &= \sum_a \left(df_a ∧ dq^a + dp_a ∧ dv^a\right) \\
&= \sum_a \left(df_a ∧ dq^a - dv^a ∧ dp_a\right) \\
&= \sum_a \left(d(f_a dq^a) - d(v^a dp_a)\right) \\
&= d\left(\sum_a \left(f_a dq^a - v^a dp_a\right)\right).
\end{align}$$
Here, some algebra for differential forms is used: $d(fdg) = df∧dg$, where $f$ is a scalar and $g$ is a differential form (which includes scalars as a special case).
A solution to this is that there be a function $H$ that has the scalar coefficients are its differential coefficients:
$$\frac{∂H}{∂q^a} = f_a, \hspace 1em \frac{∂H}{∂p_a} = -v^a.$$
So, $H$ generates a set of constitutive relations between the two sets of quantities that have $\left(q^a, p_a: a = 1, 2, 3, ...\right)$ as their independent variables.
The Hamiltonian is a function that generates constitutive relations for the $v$'s and $f$'s in terms of the $q$'s and $p$'s.
Another solution is a function $L$ that has the scalar coefficients of this
$$\begin{align}
0 &= \sum_a \left(df_a ∧ dq^a + dp_a ∧ dv^a\right) \\
&= \sum_a \left(d\left(f_a dq^a\right) + d\left(p_a dv^a\right)\right) \\
&= d\left(\sum_a \left(f_a dq^a + p_a dv^a\right)\right)
\end{align}$$
as its differential coefficients. That would be the Lagrangian:
$$\frac{∂L}{∂q^a} = f_a, \hspace 1em \frac{∂L}{∂v^a} = p_a.$$
The Lagrangian generates constitutive relations for the dynamic variables - the $p$'s and $f$'s - in terms of the kinematic variables - the $q$'s and $v$'s.
In the first case, if you substitute directly into the kinematic and dynamic laws, then you get:
$$\frac{∂H}{∂q^a} = \frac{dp_a}{dt}, \hspace 1em \frac{∂H}{∂p_a} = -\frac{dq^a}{dt},$$
while in the second case, substitution leads to:
$$v^a = \frac{dq^a}{dt}, \hspace 1em \frac{∂L}{∂q^a} = \frac{d}{dt}\left(\frac{∂L}{∂v^a}\right).$$
Other combinations are possible. You could switch some of the $p$'s and $v$'s with one another, or some of the $q$'s and $f$'s, in the equation for $_{d/dt} ω = 0$ and its integral.
Applying this to the example involving gravitational motion, in vector form, one has:
$$ = m = \frac{∂L}{∂}, \hspace 1em
= -\frac{GM}{||^2} \frac{}{||} = \frac{∂L}{∂},$$
thus,
$$dL = m·d - \frac{GM}{||^2} \frac{·d}{||} = d\left(\frac{m||^2}{2} + \frac{GM}{||}\right),$$
leading to
$$L = \frac{m||^2}{2} + \frac{GM}{||} + L_0,$$
for some constant $L_0$.
For the Hamiltonian formulation, you would have:
$$ = \frac{}{m} = \frac{∂H}{∂}, \hspace 1em
= -\frac{GM}{||^2} \frac{}{||} = -\frac{∂H}{∂},$$
thus,
$$dH = \frac{}{m}·d + \frac{GM}{||^2} \frac{·d}{||} = d\left(\frac{||^2}{2m} - \frac{GM}{||}\right),$$
leading to
$$H = \frac{||^2}{2m} - \frac{GM}{||} + H_0,$$
for some constant $H_0$.