In general the Legendre transformation$^1$ from the Lagrangian to the Hamiltonian formulation may be singular, which leads to primary constraints. This is e.g. the case for gauge theories like Yang-Mills (YM) theory with or without matter, which OP mentions.
However, in case of a singular Legendre transformation, by performing a so-called Dirac-Bergmann analysis (which may lead to secondary constraints), it is still possible in principle to define a corresponding Hamiltonian formulation. Typically, the canonical Hamiltonian $H_0=p\dot{q}-L$ gets amended with terms of the form 'constraint times Lagrange multiplier'. For details, see e.g. Refs. 1 & 2.
References:
P.A.M. Dirac, Lectures on QM, 1964.
M. Henneaux & C. Teitelboim, Quantization of Gauge Systems, 1994.
--
$^1$ Concerning fermions, see e.g. this Phys.SE post.