[proofplan]
We prove continuity by using the open-set definition. Given an arbitrary open subset of $Z$, we compute its preimage under $g \circ f$ as an iterated preimage: first under $g$, then under $f$. Continuity of $g$ makes the first preimage open in $Y$, and continuity of $f$ makes the second preimage open in $X$.
[/proofplan]
[step:Declare the composition as a map from $X$ to $Z$]
Define the composition
\begin{align*}
g \circ f: X &\to Z \\
x &\mapsto g(f(x)).
\end{align*}
This is well-defined because $f(x) \in Y$ for every $x \in X$, and $g$ has domain $Y$.
[/step]
[step:Compute the preimage of an arbitrary open set in $Z$]
Let $W \in \tau_Z$ be an arbitrary open subset of $Z$. By the definition of preimage and of composition,
\begin{align*}
(g \circ f)^{-1}(W)
&= \{x \in X : (g \circ f)(x) \in W\} \\
&= \{x \in X : g(f(x)) \in W\} \\
&= \{x \in X : f(x) \in g^{-1}(W)\} \\
&= f^{-1}(g^{-1}(W)).
\end{align*}
[guided]
We must prove that $g \circ f$ is continuous. By the [definition of continuity](/page/Continuity) for maps between topological spaces, it is enough to show that for every open subset $W$ of the codomain $Z$, the preimage $(g \circ f)^{-1}(W)$ is open in $X$.
Fix an arbitrary open set $W \in \tau_Z$. We now compute the preimage of $W$ under the composed map. Using the definition of preimage and the definition of composition,
\begin{align*}
(g \circ f)^{-1}(W)
&= \{x \in X : (g \circ f)(x) \in W\} \\
&= \{x \in X : g(f(x)) \in W\}.
\end{align*}
The condition $g(f(x)) \in W$ is equivalent to saying that $f(x)$ lies in the preimage of $W$ under $g$. Since
\begin{align*}
g^{-1}(W) = \{y \in Y : g(y) \in W\},
\end{align*}
we obtain
\begin{align*}
\{x \in X : g(f(x)) \in W\}
&= \{x \in X : f(x) \in g^{-1}(W)\} \\
&= f^{-1}(g^{-1}(W)).
\end{align*}
Thus the preimage of $W$ under the composition is exactly the preimage under $f$ of the preimage under $g$.
[/guided]
[/step]
[step:Use continuity of $g$ and then continuity of $f$]
Since $g: Y \to Z$ is continuous and $W \in \tau_Z$, the set $g^{-1}(W)$ belongs to $\tau_Y$. Since $f: X \to Y$ is continuous and $g^{-1}(W) \in \tau_Y$, the set $f^{-1}(g^{-1}(W))$ belongs to $\tau_X$. Therefore
\begin{align*}
(g \circ f)^{-1}(W) \in \tau_X.
\end{align*}
[guided]
We now use the two continuity hypotheses in the correct order. First, $W$ is open in $Z$, meaning $W \in \tau_Z$. Since $g: Y \to Z$ is continuous, the preimage of every open subset of $Z$ under $g$ is open in $Y$. Applying this to $W$, we get
\begin{align*}
g^{-1}(W) \in \tau_Y.
\end{align*}
Next, since $f: X \to Y$ is continuous, the preimage under $f$ of every open subset of $Y$ is open in $X$. The set $g^{-1}(W)$ is an open subset of $Y$, so applying continuity of $f$ gives
\begin{align*}
f^{-1}(g^{-1}(W)) \in \tau_X.
\end{align*}
From the preimage identity computed above,
\begin{align*}
(g \circ f)^{-1}(W) = f^{-1}(g^{-1}(W)),
\end{align*}
and hence
\begin{align*}
(g \circ f)^{-1}(W) \in \tau_X.
\end{align*}
[/guided]
[/step]
[step:Conclude that the composition is continuous]
Because $W \in \tau_Z$ was arbitrary, the preimage of every open subset of $Z$ under $g \circ f$ is open in $X$. By the open-set definition of continuity, $g \circ f: X \to Z$ is continuous.
[/step]