Take, for example, $f(x) = \ln(x)$ and $g(x) = e^x$.
Then, for every $x\in\mathbb R$, you have $f(g(x))=\ln(e^x) = x$. But that is not true the other way around, i.e. the statement:
For every $x\in \mathbb R: g(f(x)) = x$
is a false statement, because $f(x)$ is not defined for $x\leq0$.
If you want $f$ to be defined everywhere, you can define
$$f(x)=\begin{cases}\ln|x| & \text{if } x\neq 0\\
0&\text{if } x = 0\end{cases}.$$
You then have, for every $x\in\mathbb R$: $f(g(x)) = f(e^x)$ which, because $e^x\neq 0$, is equal to $\ln(|e^x|) = \ln(e^x)=x$.
On the other hand, you have, for every $0\neq x\in \mathbb R$,$g(f(x)) = e^{\ln|x|} = |x|$, so if $x<0$, $g(f(x))$ is not equal to $x$.