[proofplan]
We pass from $g_0 \circ f_0$ to $g_1 \circ f_1$ in two moves: first vary the inner map ($f_0 \rightsquigarrow f_1$) while holding $g_0$ fixed, then vary the outer map ($g_0 \rightsquigarrow g_1$) while holding $f_1$ fixed. Each move is implemented by a single explicit homotopy — $g_0 \circ H$ for the first, and $H' \circ (f_1 \times \mathrm{id}_I)$ for the second — continuous by composition of continuous maps. The conclusion then follows from transitivity of the homotopy relation, i.e., the [Homotopy is an Equivalence Relation](/theorems/1872) theorem.
[/proofplan]
[step:Name the given homotopies]
By hypothesis there exist continuous maps
\begin{align*}
H: X \times I &\to Y, & (x, t) &\mapsto H(x, t), \\
H': Y \times I &\to Z, & (y, t) &\mapsto H'(y, t),
\end{align*}
where $I = [0, 1]$, satisfying
\begin{align*}
H(x, 0) &= f_0(x), & H(x, 1) &= f_1(x), \quad x \in X, \\
H'(y, 0) &= g_0(y), & H'(y, 1) &= g_1(y), \quad y \in Y.
\end{align*}
(We are not assuming homotopies rel a subspace.)
[/step]
[step:Vary the inner map: produce a homotopy $g_0 \circ f_0 \simeq g_0 \circ f_1$]
Define
\begin{align*}
K: X \times I &\to Z \\
(x, t) &\mapsto g_0(H(x, t)).
\end{align*}
Equivalently, $K = g_0 \circ H$. Both $H: X \times I \to Y$ and $g_0: Y \to Z$ are continuous, so $K$ is continuous as the composition of continuous maps.
Checking the endpoints,
\begin{align*}
K(x, 0) &= g_0(H(x, 0)) = g_0(f_0(x)) = (g_0 \circ f_0)(x), \\
K(x, 1) &= g_0(H(x, 1)) = g_0(f_1(x)) = (g_0 \circ f_1)(x).
\end{align*}
Hence $K$ is a homotopy from $g_0 \circ f_0$ to $g_0 \circ f_1$, so
\begin{align*}
g_0 \circ f_0 \simeq g_0 \circ f_1.
\end{align*}
[/step]
[step:Vary the outer map: produce a homotopy $g_0 \circ f_1 \simeq g_1 \circ f_1$]
Define
\begin{align*}
L: X \times I &\to Z \\
(x, t) &\mapsto H'(f_1(x), t).
\end{align*}
Equivalently, $L = H' \circ (f_1 \times \mathrm{id}_I)$, where
\begin{align*}
f_1 \times \mathrm{id}_I: X \times I &\to Y \times I \\
(x, t) &\mapsto (f_1(x), t).
\end{align*}
The map $f_1 \times \mathrm{id}_I$ is continuous by the universal property of the product topology: $\pi_1 \circ (f_1 \times \mathrm{id}_I) = f_1 \circ \pi_1$ and $\pi_2 \circ (f_1 \times \mathrm{id}_I) = \pi_2$ are both continuous, so the pair map into the product is continuous. Composing with the continuous $H'$, we conclude $L$ is continuous.
Checking the endpoints,
\begin{align*}
L(x, 0) &= H'(f_1(x), 0) = g_0(f_1(x)) = (g_0 \circ f_1)(x), \\
L(x, 1) &= H'(f_1(x), 1) = g_1(f_1(x)) = (g_1 \circ f_1)(x).
\end{align*}
Hence $L$ is a homotopy from $g_0 \circ f_1$ to $g_1 \circ f_1$, so
\begin{align*}
g_0 \circ f_1 \simeq g_1 \circ f_1.
\end{align*}
[guided]
For the second variation we want to move from $g_0$ to $g_1$ while feeding in the fixed input $f_1(x)$ instead of a general $y \in Y$. The homotopy $H'$ lives on $Y \times I$, so we need to lift the input $x \mapsto f_1(x)$ through it — this is exactly what $f_1 \times \mathrm{id}_I$ does: it sends the time variable $t$ through unchanged and feeds $f_1(x)$ into the $Y$-slot.
Why is the product map $f_1 \times \mathrm{id}_I$ continuous? The product topology on $Y \times I$ is the coarsest topology making both projections $\pi_1: Y \times I \to Y$ and $\pi_2: Y \times I \to I$ continuous. By the universal property, a map $\phi: W \to Y \times I$ is continuous iff $\pi_1 \circ \phi$ and $\pi_2 \circ \phi$ are continuous. Here $\pi_1 \circ (f_1 \times \mathrm{id}_I) = f_1 \circ \pi_1: X \times I \to Y$ is the composition of two continuous maps, hence continuous, and $\pi_2 \circ (f_1 \times \mathrm{id}_I) = \pi_2: X \times I \to I$ is itself a projection, hence continuous. Therefore $f_1 \times \mathrm{id}_I$ is continuous.
The whole homotopy $L = H' \circ (f_1 \times \mathrm{id}_I)$ is then the composition of two continuous maps, hence continuous.
An alternative way to see the same homotopy: for each fixed $x \in X$, the slice $t \mapsto L(x, t)$ traces out the path $t \mapsto H'(f_1(x), t)$ from $g_0(f_1(x))$ to $g_1(f_1(x))$ in $Z$. As $x$ varies, these paths vary continuously with $x$ because $H'$ is continuous and $f_1$ is continuous. The compact formulation using the product map makes the joint continuity precise.
[/guided]
[/step]
[step:Conclude by transitivity of homotopy]
The [Homotopy is an Equivalence Relation](/theorems/1872) theorem states that $\simeq$ is an equivalence relation on $\mathrm{Map}(X, Z)$, so in particular it is transitive.
Combining Step 2 and Step 3,
\begin{align*}
g_0 \circ f_0 \simeq g_0 \circ f_1 \simeq g_1 \circ f_1,
\end{align*}
and transitivity yields
\begin{align*}
g_0 \circ f_0 \simeq g_1 \circ f_1,
\end{align*}
which is the claim.
[/step]