On a weaker version of homotopy equivalence between topological spaces
Saying that there are maps $f \colon X \to Y$ and $g \colon Y \to X$ such that $g f$ is homotopic to $\mathrm{id}_X$ means that $X$ is a homotopy retract of $Y$. (By the way, we say that maps are homotopic not homotopy equivalent.) In your situation you have two spaces that are homotopy retracts of each other and it turns out that they need not be homotopy equivalent.
Let $T^k$ be the $k$-torus (the product of $k$ copies of $S^1$). Set $X = \bigsqcup_{k \ge 0} T^k$ and $Y = \bigsqcup_{k \ge 1} T^k$. Let $f \colon X \to Y$ map each $T^k$ onto $T^k \times \{ 1 \} \subset T^{k + 1}$, this has a (strict) retraction that is given by projections $T^{k + 1} \to T^k$ (on the first $k$ coordinates). Let $g' \colon Y \to X$ map each $T^k$ identically onto $T^k$, it also has a (strict) retraction that just maps $T^0$ anywhere. However, $X$ and $Y$ are not homotopy equivalent since $X$ has a contractible component, but $Y$ does not.
In any category, we can say that a biretraction between objects $X$ and $Y$ is a system of maps $(f,g,f',g')$ with $gf=1_X$ and $f'g'=1_Y$, and we can say that two objects are biretractible if there is a biretraction between them. Biretractions can be composed and reversed in an obvious way, so biretractibility is an equivalence relation.
We say that a biretraction "is an isomorphism" if $f$, $g$, $f'$ and $g'$ are isomorphisms, or equivalently $fg=1_Y$ and $g'f'=1_X$.
In any category with good finiteness properties, it will work out that every biretraction is an isomorphism. For example, in the category of finite-dimensional vector spaces over a field this follows easily from consideration of dimensions, and a slightly harder argument gives the same conclusion for finitely generated modules over a Noetherian ring. (A standard lemma shows that any surjective endomorphism of such a module is an isomorphism.) Now consider the homotopy category of those simply connected CW complexes $X$ for which all homology groups $H_k(X)$ are finitely generated. Any biretraction in this category will give a biretraction of homology groups, which will necessarily be an isomorphism, so the original biretraction is an isomorphism by Whitehead's theorem. Thus, biretractibility is the same as homotopy equivalence in this context, and many similar contexts.
Of course, Karol's example has no good finiteness properties and so is not covered by this analysis.
For a slightly different example, in the category of (possibly infinite) sets, a biretraction need not be an isomorphism, but nonetheless biretractible objects are isomorphic, by the Schröder–Bernstein theorem.