I have written a proof, and I would appreciate verification. The problem is picked from "Set Theory and Matrices" by I. Kaplansky.
Proof. Let $a_1=f(x)$ and $a_2=f(y)$, then
$g(a_1)=g(a_2) \Longrightarrow g(f(x))=g(f(y)) \overset{def}{\Longrightarrow} x=y \Longrightarrow f(x)=f(y) \Longrightarrow a_1=a_2$
Therefore, according to transitivity, we get that $\,g(a_1)=g(a_2) \Longrightarrow a_1=a_2$, which is (one of) the definition(s) of an injective function. Thus, $g$ is injective if $gf$ is injective. QED
My insecurity is foremost the first step as well as some unclarity about explicitly highlight that $f$ is surjective.
First be precise about what you are proving: Let $f\colon X\to Y$ be a surjective map and $g\colon Y\to Z$ be a map such that $g\circ f$ is injective. Then $g$ is injective.
In your proof you never explicitly state where you are using the fact that $f$ is surjective. I would rewrite the first step as follows. Suppose that $g(a_1)=g(a_2)$ for some $a_1, a_2\in Y$. Since $f$ is surjective there exists $x, y\in X$ such that $f(x)=a_1$ and $f(y)=a_2$.
Then we can proceed with your proof. I would also recommend using words rather than symbols when writing proofs.
So to continue with the proof I would write, it follows that $g(f(x))=g(f(y))$. Since $g\circ f$ is injective it is the case that $x=y$ and hence $a_1=a_2$.