- $f$ has to be injective, because otherwise there would exist distinct $x,x' \in X$ such that $f(x)=f(x')$, and hence $g(f(x))=g(f(x'))$, implying $x=x'$, a contradiction.
- Hence $f$ can't be surjective, because if it was then $f$ would be a bijection, and hence invertible.
What's the simplest way this can happen? Make $X=\{a\}$ and $Y=\{b,c\}$. Define $f(a)=b$ and $g(b)=g(c)=a$. Then we claim $g \circ f = id_{X}$. Well, we just need to check $(g \circ f)(a)=a$. But that's easy: $g(f(a))=g(b)=a$. Done.
Such a $g$ will always exist if $f$ is injective. Why? Well being injective means that if we know $f(x)$, we can find $x$. In other words there is a function $h$ from the set $\{f(x) | x \in X\}\subset Y$ (the image of $f$), to $X$, which sends $f(x)$ to $x$. The condition of injectivity is precisely that this function $h$ is well-defined.
But that hasn't quite answered the question, because we wanted a function $g :Y \to X$. So we need to define $g$ on all the points of $Y$ that $f$ doesn't hit. But who cares! $g \circ f$ doesn't 'see' points in $Y$ which aren't in the image of $f$. So define $g$ to be whatever you like there. (Note that if $Y$ has at least one point not in the image of $f$, and $X$ has more than one point, then the choice of $g$ will not be unique).