while I was solving some exercises related to Rolle's theorem or the intermediate value theorem I realized that the trick is always knowing how to choose the function, for example, to show that every continuous function has a fixed point ($f(c) =c$) we take $g(x)=f(x)-x$ and apply the mean value theorem. Another example is to prove that $|\cos(x)-\cos(y)|\leq |x-y|$, in this case we define $f(t)=\cos(t)$ and extend the intermediate value theorem. Even to prove the Intermediate Value Theorem you can use the function $g(x)=f(x)-f(a)-\frac{f(b)-f(a)}{b-a} (x-a)$ and We apply Rolle's theorem.
My question is, is there any advice or "trick" to quickly choose the function that will facilitate the demonstration?
There is no master trick for all situations. The common theme here is "how can I reduce my problem to the one I know how to solve".
In your first example, you transfer the problem of finding a fixed point to the equivalent problem of finding a root. Although they are equivalent, continuity is very helpful in determining if there is a root in an interval or not: if $f(a)$ and $f(b)$ are of opposite signs, there is a root of $f$ in $(a,b)$ because continuous functions map segments to segments (IVT).
I don't really see what exactly is the "trick" in your second example, you are proving a property of cosine, you didn't define some "wild" auxiliary function.
Finally, for the Lagrange's mean value theorem, peek-a-boo already gave you the idea in the comments. In Rolle's theorem you take roots $a$ and $b$ of $f$ and a line $l$ through $(a,f(a))$ and $(b,f(b))$ and you say that there's a point $c$ such that tangent line of $f$ at $c$ is parallel to $l$. This is long-winded way to say $f'(c) = 0$, but it's helpful because that's what the mean value theorem says as well. It just loses the assumptions that $a$ and $b$ are roots of $f$.
So, we want to modify $f$ in two ways, to get a function with roots $a$ and $b$ and do that in a way that won't deform $f$ too much so we can't extract its derivative from the new function. First we can make $a$ a root by defining $g(x) = f(x) - f(a)$, but still, $b$ won't be a root in general. So, we need $g(x) = f(x) - f(a) + h(x)$ where $h(a) = 0$ and $h(b) = f(a)-f(b)$. What's the simplest $h$ you can think of with these properties? Well, take a line $h(x) = k(x-a)$ and solve for $k$. Coincidentally, since derivative of a line is constant, we didn't deform $f$ too much.
We could also just say: well, take the line $h(x)$ through points $(a,f(a))$ and $(b,f(b))$ and subtract it from $f$. This will work since we get $a$ and $b$ to be roots and derivative of $h$ is constant.
Proofs in analysis take time to get used to. It always helps to think of them geometrically when possible and visualize what exactly are we trying to accomplish. The rest is a matter of technique how to actually achieve it, and you get that by going through lots of proofs until it becomes natural. This is precisely why students learn proofs. Many of them you will forget, sooner than you'd like. But the main ideas will stay and with time you will build up a tool set you'll be able to apply to new problems.