Let us suppress explicit time dependence $t$ from the notation in the following. Hamilton's eqs. are the Euler-Lagrange (EL) eqs. for the so-called Hamiltonian Lagrangian
$$\tag{1} L_H(q,\dot{q},p)~:=~ p_i\dot{q}^i-H(q,p).$$
In other words, the solutions to Hamilton's eqs. are stationary points for the Hamiltonian action
$$\tag{2} S_H[q,p]~:=~\int \! dt~L_H(q,\dot{q},p). $$
Next define the Lagrangian as
$$ \tag{3} L(q,\dot{q})~:=~\sup_p L_H(q,\dot{q},p). $$
Formula (3) is the succinct answer to OP's question about how to construct the Lagrangian from the Hamiltonian.
The Legendre transformation (3) is often referred to as integrating out$^1$ the momentum variables $p_i$. Then the action (2) becomes
$$ \tag{4} S[q]~:=~\int \! dt~L(q,\dot{q}). $$
The stationary points of the action (4) are given by the EL eqs. for $L$.
--
$^1$ If we go beyond classical mechanics, and consider the phase space path integral formulation, then "integrating out the momentum" is exactly what is happening.