The converse seems to be false. Let $G=\mathbb{Z}$, and let $g$ be the identity morphism. Let $H$ be the group $\prod_{n\in \mathbb{N}}\mathbb{Z}/n\mathbb{Z}$, and let $f:G\to H$ be the morphism sending each $m$ to the element of $H$ whose $n$-th coordinate is the image of $m$ in $\mathbb{Z}/n\mathbb{Z}$ by the natural projection. Then each $g_n$ factors through $H$: it is the composition of $f$ with the canonical projection from $H$ to its $n$-th direct factor; indeed, the composition $$ \mathbb{Z}\stackrel{f}{\to} \prod_{n\in \mathbb{N}}\mathbb{Z}/n\mathbb{Z} \to \mathbb{Z}/n\mathbb{Z} $$ is then the natural projection from $\mathbb{Z}$ to $\mathbb{Z}/n\mathbb{Z}$, which is equal to $g_n$ by definition.
However, $g$ itself does not factor through $H$. Indeed, there are no non-zero morphisms from $H=\prod_{n\in \mathbb{N}}\mathbb{Z}/n\mathbb{Z}$ to $\mathbb{Z}$. To see this, consider the morphism $\prod_{n\in \mathbb{N}}\mathbb{Z}\to \prod_{n\in \mathbb{N}}\mathbb{Z}/n\mathbb{Z}$ given by the componentwise projection. The composition $$\prod_{n\in \mathbb{N}}\mathbb{Z}\to \prod_{n\in \mathbb{N}}\mathbb{Z}/n\mathbb{Z} \to \mathbb{Z}$$ would be a morphism whose kernel contains $\bigoplus_{n\in \mathbb{N}}\mathbb{Z}$. By a theorem of Specker (see, for instance, answers to this question), this implies that the composition is zero. The left morphism being surjective, this in turn implies that any morphism $\prod_{n\in \mathbb{N}}\mathbb{Z}/n\mathbb{Z} \to \mathbb{Z}$ vanishes.
This proves that $g$ does not factor through $H$ in this example.