I am trying to prove that group P is free if and only if it satisfies the following:
for any groups $A,B$, any homomorphism $\pi: P \rightarrow B$ and any epimorphism $\alpha: A \rightarrow B$ there exists homomorphism $\phi: P \rightarrow A$ that $\alpha \phi=\pi$.
I have proven that any free group satisfies that but I can't prove another inclusion.
Let $P$ be a group with the property you mention. (I think this means $P$ is a projective object in the category $\mathsf{Grp}$) There exists a free group $F$ with a surjection $\alpha:F \rightarrow P$. Now take $\pi = \text{id}_P : P \rightarrow P$. Then by the assumed property there exists a homomorphism $\phi:P \rightarrow F$ such that $\alpha \phi = \text{id}_P$. Hence we can identify $P$ with a subgroup of $F$. Since subgroups of free groups are free, we are done.