When does an injective group homomorphism have an inverse?
If and only if $B$ is a semidirect product of $A$ and another group (the latter is normal). One direction is obvious, and another direction is easy: $B$ is a semidirect product of the image of $f$ and the kernel of $g$.