Consider $f:\Bbb R \times \Bbb R \to \Bbb R$ and $g:\Bbb R \to \Bbb R \times \Bbb R $ to be defined by $$ f(x,y) = x \qquad g(x) = (x,0) $$ Surely, we have $f \circ g = \operatorname{id}_{\Bbb R}$. However, we have $$ [g \circ f](x,y) = (x,0) $$ which is certainly not the identity on $\Bbb R \times \Bbb R$. I would say it is "almost never" the identity function in the sense that the $x$-axis is an "infinitely small fraction" of the entire $xy$-plane.


It is notable that if $f \circ g = \operatorname{id}$, then $g \circ f$ will always be equal to the identity over the range of $g$. In particular: for any $y$ in the range of $g$, we can write $y = g(x)$ for some $x$ in the domain of $g$, and so we have $$ [g \circ f](y) = [g \circ f](g(x)) = g([f \circ g](x)) = g(\operatorname{id}(x)) = g(x) = y $$ You may want to verify that $f$ will have a "left-inverse" in this sense if and only if it is injective (one-to-one), just as $g$ will have a "right-inverse" in this sense if and only if it is surjective (onto).

We can conclude from all this that if $f,g: \Bbb R \to \Bbb R$ are such that $f \circ g = \operatorname{id}$, then we will have $g \circ f = \operatorname{id}$ on at least some uncountable subset of $\Bbb R$.


$$ f(x) = \dfrac{x}{1+|x|} , \hspace{0.1 in} g(x) = \begin{cases} \frac{x}{1-|x|}\, & |x|<1 \\ 0 & |x|\ge 1 \end{cases}\,. $$ $g\circ f = \text{id}$ but $f\circ g$ is not the identity function.


Let $g$ be $\arctan$ and let $f$ be $\tan$ when defined and $17$ for the rest of the inputs (for $\dfrac{\pi}{2}+k\pi$ for integers $k$). Then $f\circ g=\mathrm{id}_{\mathbb R}$ but $g\circ f(x)=x$ only for $x\in\left(-\dfrac{\pi}{2},\dfrac{\pi}2\right)$.


The existence of $g$ such that $f\circ g=\operatorname{id} $ means (is equivalent to) $f$ is surjective, such that $g\circ f=\operatorname{id} $ means $f$ is injective. Why do you want being surjective is equivalent to being injective in general?

For instance, the first projection $\;\begin{aligned}[t]\mathbf R^2&\longrightarrow\mathbf R\\(x,y)&\longmapsto x\end{aligned}$ has a right inverse, which is the canonical injection $\begin{aligned}[t]\mathbf R&\longrightarrow\mathbf R^2\\x&\longmapsto (x,0)\end{aligned}$, and the composition of both is indeed the identity of $\mathbf R$. But the composition the other way yields the map $(x,y)\longmapsto (x,0)$.