[guided]First rewrite the Hilbert form directly. By definition,
\begin{align*}
\omega=L(x,y,q(x,y))\,dx+P(x,y)\cdot(dy-q(x,y)\,dx).
\end{align*}
Expanding gives
\begin{align*}
\omega=\bigl(L(x,y,q(x,y))-P(x,y)\cdot q(x,y)\bigr)\,dx+P(x,y)\cdot dy.
\end{align*}
Since
\begin{align*}
H(x,y,P(x,y))=P(x,y)\cdot q(x,y)-L(x,y,q(x,y)),
\end{align*}
this becomes
\begin{align*}
\omega=-H(x,y,P(x,y))\,dx+P(x,y)\cdot dy.
\end{align*}
The coefficient $P$ is $C^1$. The coefficient $-H(x,y,P(x,y))$ is the same as $L(x,y,q(x,y))-P(x,y)\cdot q(x,y)$, so it is $C^1$ because $L$ is $C^2$ and $q$ and $P$ are $C^1$. Thus $\omega$ is a $C^1$ one-form. The closedness condition $d\omega=0$ is exactly the integrability condition for finding a potential. Since $D$ is simply connected, the exactness theorem for closed $1$-forms applies and gives $S$ with
\begin{align*}
dS=\omega.
\end{align*}
Now the whole argument is coefficient comparison. The differential of $S$ is
\begin{align*}
dS=\partial_xS\,dx+\nabla_yS\cdot dy.
\end{align*}
The Hilbert form in Hamiltonian coordinates is
\begin{align*}
\omega=-H(x,y,P(x,y))\,dx+P(x,y)\cdot dy.
\end{align*}
Equality of these two $1$-forms forces the $dy$ coefficients to agree, so
\begin{align*}
\nabla_yS=P.
\end{align*}
It also forces the $dx$ coefficients to agree, so
\begin{align*}
\partial_xS=-H(x,y,P).
\end{align*}
Replacing $P$ by $\nabla_yS$ gives the Hamilton-Jacobi equation
\begin{align*}
\partial_xS+H(x,y,\nabla_yS)=0.
\end{align*}[/guided]