One (and the simplest) way of getting to equations of motion in Hamiltonian mechanics is through calculus of variation. It is illuminating enough to consider 1-D senario at the beggining, since the higher dimensional case merely needs adding summation. First, we know that by definition:

$$

\mathcal{H} = p\dot{q} - \mathcal{L}

$$

where $q$ is generalized coordinates and $p$ is the corresponding generalized momentum. Of course, $\mathcal{L}$ is our old friend *Lagrangian* $\mathcal{L}(q,\dot{q},t) $.

Then, if we consider imposing variation on $q$ and $p$ so that, if we denote the true path of them as:

$$

\begin{cases}

q(t)\\

p(t)

\end{cases}

$$

We can impose also a variation on them:

$$

\begin{cases}

q_\alpha(t) = q(t) + \gamma_\alpha(t) \\

p_\alpha(t) = p(t) + \gamma^\prime_\alpha(t)

\end{cases}

$$

Here, we can thing of $gamma$'s as the difference between true path and altered path at any given time. If our system begins at $t_1$ and ends at $t_2$, we naturally have:

$$

\gamma_\alpha(t_1) = \gamma_\alpha(t_2) = 0

$$

Because the two ends are known to us. Now, by definition of action, we see that the variation of action is:

$$

\delta \mathcal{S} = d\alpha \frac{\partial}{\partial \alpha}\int_{t_1}^{t_2} dt \mathcal{L} = d\alpha \frac{\partial}{\partial \alpha}\int_{t_1}^{t_2} dt \left[ p_\alpha\dot{q}_\alpha - \mathcal{H} \right]

$$

Now we want to move the partial derivative into the integration because the variation we impose is not related to time, also bear in mind that the partial derivative would hit term and we will be using chain rule here:

$$

\begin{split}

\delta \mathcal{S} &=d\alpha \frac{\partial}{\partial \alpha}\int_{t_1}^{t_2} dt \left[ p_\alpha\dot{q}_\alpha - \mathcal{H}(p,\dot{p}) \right] \\

&=d\alpha\int dt \left[ \frac{\partial p_\alpha}{\partial \alpha}\dot{q}_\alpha +

p_\alpha\frac{\partial \dot{q}_\alpha}{\partial \alpha} -

\frac{\partial \mathcal{H} }{\partial q_\alpha}\frac{\partial q_\alpha}{\partial \alpha}-

\frac{\partial \mathcal{H}}{\partial p_\alpha}\frac{\partial p_\alpha}{\partial \alpha} \right]

\end{split}

$$

Then if we apply inverse by parts to the second term in the bracket, we see that:

$$

\int_{t_1}^{t_2}dt p_\alpha\frac{\partial \dot{q}_\alpha}{\partial \alpha} =

\left[ p_\alpha\frac{\partial q_\alpha}{\partial \alpha}\right]_{t_1}^{t_2}-

\int_{t_1}^{t_2}dt\dot{p}_\alpha\frac{\partial q_\alpha}{\partial \alpha}=0-\int_{t_1}^{t_2}dt\dot{p}_\alpha\frac{\partial q_\alpha}{\partial \alpha}

$$

The term evaluated to zero because we know the ends have no variation.

Plug this result back into the original integration we see that:

$$

0=\delta\mathcal{S}=d\alpha\int dt \left[

\frac{\partial p_\alpha}{\partial \alpha}\dot{q}_\alpha -\dot{p}_\alpha\frac{\partial q_\alpha}{\partial \alpha} -

\frac{\partial \mathcal{H} }{\partial q_\alpha}\frac{\partial q_\alpha}{\partial \alpha}-

\frac{\partial \mathcal{H}}{\partial p_\alpha}\frac{\partial p_\alpha}{\partial \alpha} \right]

$$

If we re-group terms with the same partial derivative together, and argue that since those derivative are not necessariley zero, we must have:

$$

\begin{cases}

\dot{q} = \frac{\partial \mathcal{H}}{\partial p}\\

\dot{p} = - \frac{\partial \mathcal{H}}{\partial q}

\end{cases}

$$

Which is what we wanted.