A map $g$ will exist, and be given by
$$
g(x) = q\circ f\circ q^{-1}(x)
$$
if and only if the above map is well defined. What I mean is this: there may be many elements in $q^{-1}(x)$, but as long as any choice of $s\in q^{-1}(x)$ leads to the same value of $q\circ f(s)$, then $q\circ f\circ q^{-1}$ is well defined. Another way of saying this is that $q\circ f$ must be constant on the sets $q^{-1}(x)$ for any $x\in\mathbb{R}$.
When $g$ is well defined, you can indeed show $g$ makes the diagram commute. When it is not well defined, there must exists $s,t\in q^{-1}(x)$, for some $x\in\mathbb{R}$, where $q\circ f(s)\neq q\circ f(t)$. Since, for any $g$, $g\circ q(s)=g(x)=g\circ q(t)$, the diagram cannot commute, since $g\circ q$ maps $s,t$ to the same place, while $q\circ f$ maps them to different places.