Take fg=f∘g. The assertion can fail on an infinite set. On X=N0, setg(n)=n+1, f(0)=0, and f(n)=n−1 for n≥1. Then f(g(n))=n for every n, but g(f(0))=1=0. Thus a right inverse need not be a left inverse.
On a finite set it is always true. From fg=idX, g is an injection: equality of two g-values gives equality after applying f. An injective self-map of a finite set is a bijection. Composing fg=idX with g−1 shows f=g−1, and hence gf=idX. This includes the empty set.