In contrast to Lagrangian mechanics, where the Lagrangian is a function of the coordinates and their velocities, the Hamiltonian uses the variables q and p, rather than velocity.
The Hamiltonian is a function used to solve a problem of optimal control for a dynamical system.
It can be understood as an instantaneous increment of the Lagrangian expression of the problem that is to be optimized over a certain time period.
The motion of a dynamical system in a given time interval is such as to maximize or minimize the action integral. (In practice, the action integral is almost always minimized.) This statement is known as Hamilton's principle, and was first formulated in 1834 by the Irish mathematician William Hamilton.