The other answers have explained the issue in a mostly mathematically formal and rigorous way. I want to add to the discussion by instead trying to explain why the Lagrange equations of motion stay the same in an informal yet hopefully intuitive way and then discuss what is different for Hamiltons equations of motion.
First let us deal with Lagrange. Let us remember what the actual problem is. We have an action of the form $S=\int dt L$ and we want to find the trajectory for which this action is extremal. Here the Lagrangian $L$ is (for a given point in time) a function of properties of the trajectory like the actual physical location and velocity. To actually do a computation we choose coordinates to describe the physical space, call them $q(t)$, where the dependence of the Lagrangian on the physical properties of the trajectory (and time but let us assume from now on that we do not have an explicit time dependence) can now be written down as $L(q(t),\dot{q}(t),t)$ (I only look at a 1D problem, the generalization to more dimensions is conceptually easy and this way I have less to type). Of yourse our choice of coordinates does not influence the solution. If we instead take different coordinates $Q(t)$, we will still get the same trajectory, just in these different coordinates, because the problem we are solving is the same. We just have to know the changes of coordinates $Q(q)$ and $q(Q)$ and the Lagrangian must now be written as $L(q(Q(t)),\dot{q}(Q(t),\dot{Q}(t)))$ where the dependence $\dot{q}(Q(t),\dot{Q}(t))$ can be found by taking the time derivative of $q(Q(t))$.
Actually we even know how to solve the problem: The Lagrange equation of motion which as a mathematical fact is also known as the Euler-Lagrane equation. This equation states that the integral $\int dt G(x(t),\dot{x}(t))$ becomes extremal if $$\frac{d}{dt}\frac{\partial G}{\partial \dot{x}}-\frac{\partial G}{\partial x}=0$$
It has been derived by only using the information that the integral (which in our case is the action) is extremal. So if two people solve the problem, person 1 with the $q$-coordinates, person 2 with $Q$, both know that they can use the Euler Lagrange equation in their respective coordinates and both will get the same result, just each in their coordinates, because both solved the same problem.
But why isn't that the case for Hamiltons equations of motion? To see that let us have a look on how we go from Lagrange to Hamilton. The very basic idea is that we introduce a "new" variable $p$. This is of course not really new, it is in fact definied to be $p=\frac{\partial L}{\partial \dot{q}}$, so at each point in time it is a function of $q(t)$ and $\dot{q}(t)$, i.e. $p(q,\dot{q})$. We assume that this dependence can be inverted to find $\dot{q}=f(q,p)$ for some function $f$. Now we can just write our action as $S=\int dt L(q(t),f(q(t),p(t)))$ and again want to find functions $q(t)$ and $p(t)$ that make it extremal, right? No, that would be wrong. At least if we now vary $p$ and $q$ independently, we will not get the correct result. Instead we have to respect the condition that $\dot{q}=f(q,p)$. So now we don't just have an optimization problem, but an optimization problem with constraints, namely that $\dot{q}=f(q,p)$. To implement this constraint one could use the method of Lagrange multipliers, i.e. one adds to $L$ a term like $\lambda(\dot{q}-f(q,p))$ where $\lambda$ is this Lagrange multiplier. But that is not the only possibility. Instead we add a term $p(\dot{q}-f(q,p))$. One can verify that this results in the correct equation of motion for $q$ when varying $p$ and $q$ independently, i.e. using the Euler-Lagrange equation for $$\int dt (L(q(t),f(q(t),p(t)))+p(t)(\dot{q}(t)-f(q(t),p(t))))$$
Reordering the terms and defining $H(q,p)=pf(q,p)-L(q,f(q,p))$ we have rewritten our initial problem now as the optimization of $$\int dt (p(t)\dot{q}(t)-H(q(t),p(t))$$
Here $q$ and $p$ get varied independently. If we do that we get Hamiltons equation of motion.
But where does our argument from above now fail when we consider a "change of coordinates" $Q(q,p)$, $P(q,p)$? We still have an optimization problem as before. But now the difference is that, where before we had a completely arbitrary function in the integral and just plugged that into the Euler-Lagrange equation to get Lagranges equation of motion, we now rely on the integrand having a specific form. This specific form will not generally be preserved when changing coordinates. So of course we can still use the Euler-Lagrange equations to extremize the integral, but we will not necessarily get Hamiltons equations in the new coordinates. Let's see what happens:
As before the change of coordinates can be implemented by replacing the old variables by the corresponding expressions depending on the new variables, i.e. $q\to q(Q,P)$ and $p\to p(Q,P)$, leading to $$ \int dt (p(Q(t),P(t))\dot{q}(Q(t),P(t))-H(q((Q(t),P(t))),p((Q(t),P(t))))$$ having to be optimized. Observe that it does not have the form $$\int dt (P(t)\dot{Q}(t)-H(q((Q(t),P(t))),p((Q(t),P(t))))$$ which would be the expression leading to Hamiltons equations in the new coordinates. In fact the difference between the integrand we have and the integrand giving us Hamiltons equations is $p(Q(t),P(t))\dot{q}(Q(t),P(t))-P(t)\dot{Q}(t)$.
There are of course cases where this difference does not impact the actual equations. A sufficient condition for that is that it is a total time derivative, i.e. something like $\frac{d}{dt} F(Q(t),P(t))$ for some function $F$. This leads to the same equation of motion because when integrating a time derivative one just gets the function $F$ evaluated at the boundaries of the intervall of integration (which I omitted everywhere) and the variations of $q,p$ are zero at the boundary (this is used in the derivation of Lagranges equation of motion, if you don't remember it, review that part!), so the integral of the time derivative does not change under the variation.
By the way: This condition is sufficient, but not necessary. For example you could imagine adding a term to the integrand which is proportional to the integrand itself. This would of course also leave the equations of motion invariant. But in the case of canonical transformations it is generally assumed that the difference is a total time derivative.
So now we know that if $p(Q(t),P(t))\dot{q}(Q(t),P(t))-P(t)\dot{Q}(t)$ ist a total time derivative, i.e. equal to $\frac{\partial F}{\partial Q}\dot{Q}(t)+\frac{\partial F}{\partial P}\dot{P}(t)$, then we definitely get Hamiltons equations in both coordinate systems. One can also see that by plugging $\frac{\partial F}{\partial Q}\dot{Q}(t)+\frac{\partial F}{\partial P}\dot{P}(t)$ into the Euler-Lagrange equation for $P,Q$ which gives trivially zero, without restricting $P,Q$ in any way.
Because the mixed second derivatives have to be equal, i.e. $\frac{\partial^2 F}{\partial Q\partial P}=\frac{\partial^2 F}{\partial P\partial Q}$ one obtains from $p(Q(t),P(t))\dot{q}(Q(t),P(t))-P(t)\dot{Q}(t)=\frac{\partial F}{\partial Q}\dot{Q}(t)+\frac{\partial F}{\partial P}\dot{P}(t)$ the condition $\frac{\partial q}{\partial Q}\frac{\partial p}{\partial P}-1=\frac{\partial p}{\partial Q}\frac{\partial q}{\partial P}$ which can be rewritten in the more familiar form $\{q,p\}_{Q,P}=1$. This shows that this Poisson bracket being equal to 1 (i.e. the change of coordinates being a canonical transformation) is a sufficient condition for Hamiltons equations to be preserved.