Proof of the universal property of the quotient topology

For $x\in X$ let $[x]$ denote that $\sim$-equivalence class of $x$; $X/{\sim}=\{[x]:x\in X\}$. To show that such an $f$ exists, we simply define it: for $[x]\in X/{\sim}$ let $f([x])=g(x)$. Now use the fact that $g$ is constant on $[x]$ to show that $f$ is well-defined.

To show that $f$ is unique, suppose that $h:X/{\sim}\to Z$ is continuous and satisfies $g=h\circ\pi$. Let $[x]\in X/{\sim}$ be arbitrary. Then

$$f([x])=(f\circ\pi)(x)=g(x)=(h\circ\pi)(x)=h([x])\;,$$

and hence $f=h$.

To show that $f$ is continuous, let $U$ be an open set in $Z$. Show that $$f^{-1}[U]=\{[x]\in X/{\sim}:x\in g^{-1}[U]\}\;,$$ and then use the fact that $X/{\sim}$ bears the quotient topology to conclude that $f^{-1}[U]$ is open in $X/{\sim}$.