I've just started to explore the category theory, so my question and reasoning might be trivial... Anyway im reading Conceptual Mathemathics by Lawvere and Schanuel. There is an excersise (session 5, p. 70):
Does the following hold? If for any pair $a_1$, $a_2\in A$ we have $fa_{1}=fa_{2}\implies ha_{1}=ha_{2}$, then there exists $g$ such $h=gf$.
To avoid the confusion, these are the mentioned mappings of sets: $f:A\to B$, $h:A\to C$, $g:B\to C$.
I would take $g=hf^{\prime}$, where $f^{\prime}x\in f^{-1}x$ (preimage of x). If $f^{-1}x$ is empty, then $gx$ may be arbitrary. I am not one hundred percent sure if I did not overlook something. But even in the case I did not, this approach is non-constructive in a sense, that there could be many such mappings and every time we can end up constructing different one.
Bottom line is that, I cannot construct appropriate diagram... Does this mean, that in category theory context, the answer is negative? Or maybe my construction is wrong and there is a counterexample?
Your "construction" works just fine for the following reasons.
If $f^{-1}x$ is not empty then $gx$ is independent of the choice $f'x \in f^{-1}x$ by the assumption $fa_1 = fa_2 \Rightarrow ha_1 = ha_2$ for $a_1,a_2 \in A$.
For $a \in A$ we have $f(f'f(a)) = f(a)$ by the definition of $f'$, so $gf(a) = hf' f(a) = h(a)$ by the assumption.
The question is not asking for a constructive proof.
The question is not asking for uniqueness of $g$. In fact, the conditions $h = gf$ only tells us what $g$ has to do with elements in the image of $f$ and there we have no choice but we can choose to map all other elements in an arbitrary fashion.
As an example for 3. you could look at $A = B = C = \{0,1\}$ and take $f = g$ mapping $0$ and $1$ both to $1$. Then $h$ can be either the identity or also the map sending $0$ and $1$ to $1$.
Note that this result is something which works in the category of sets but not in arbitrary categories (where we first of all would have to make sense of the assumption by replacing $a_1,a_2 \in A$ with morphisms of some kind). For example, if we restrict to the special case where $f$ is a monomorphism and $h$ is the identity, the result tells us that every monomorphism is a section (has a left-inverse) which is not true in general categories (for example in the category of groups).