What is the datum of an $S$-morphism of schemes? Suppose $X \to S$ and $Y \to S$ are schemes over $S$. Hartshorne's book defines an $S$-morphism from $X$ to $Y$ as a morphism $X \to Y$ compatible with the given morphisms to $S$.
I'm not 100% sure what this means.

My thoughts:
We have continuous maps $f: X \to Y$, $g: X \to S$, $h: Y \to S$ such that $g=hf$.
And we have maps $f^\#: \mathcal O_Y \to f_* \mathcal O_X$, $g^\#: \mathcal O_S \to g_*\mathcal O_X$, $h^\#: \mathcal O_S \to \mathcal h_* \mathcal O_Y$.
I would think we should have $g^\# = f^\# h^\#$, but this does not make sense.
What are the details of the datum of an $S$-morphism?
 A: To say that a morphism of schemes $f\colon X\to Y$ is compatible with the structure maps $g\colon X\to S$ and $h\colon Y\to S$ just means that $g = hf$ as morphisms of schemes.
To unpack this, we just need to know how to compose morphisms of schemes.
The morphism of schemes $f$ is really a pair $(f,f^\#)$ consisting of a continuous map of topological spaces $f\colon X\to Y$ together with a map of sheaves of rings $f^\#\colon \mathcal{O}_Y\to f_*\mathcal{O}_X$ satisfying the locality condition. We'll use the same notation for $g$ and $h$.
Now the key observation is that the pushforward of sheaves along a continuous map is functorial. So we obtain a map of sheaves of rings $h_*f^\#\colon h_*\mathcal{O}_Y\to h_*f_*\mathcal{O}_X$. Note that the codomain is equal to $(hf)_*\mathcal{O}_X$: the composition of pushforwards is the same as the pushforward along the composition. The resulting map can be composed with $h^\#\colon \mathcal{O}_S\to h_*\mathcal{O}_Y$ to obtain a map $(hf)^\#\colon \mathcal{O}_S\to  (hf)_*\mathcal{O}_X$. The composite morphism of schemes is $(hf,(hf)^\#)$.
Ok, now let's make sure the equality $g = hf$ makes sense. Unpacking, it means that:

*

*$g = hf$ as continuous maps of topological spaces.

*$g^\# = (hf)^\#$ as maps of sheaves. The first is a map $\mathcal{O}_S\to g_*\mathcal{O}_X$, and second is a map $\mathcal{O}_S\to  (hf)_*\mathcal{O}_X$, but since we are already assuming that $g = hf$, these maps have the same codomain, so it makes sense to require them to be equal.


Edit: After I posted my answer, you edited the question to ask "what are the details of the datum of an $S$-morphism?". To be clear, the datum of an $S$-morphism $X\to Y$ is exactly the same as the datum of a morphism of schemes $X\to Y$. We just require an extra property that this datum must satisfy: compatibility with the given morphisms to $S$ as I described above.
A: A $S$-morphism $f : X \to Y$ is a morphism of schemes $f : X \to Y$ such that $hf=g$. This says that $hf=g$ as continuous maps, and $g^\# : O_S \to g_\ast O_X$ is the same as $(h_\ast f^\#) \circ h^\# : O_S \to h_\ast O_Y \to \underset{g_\ast O_X}{\underbrace{f_\ast h_\ast O_X}}$
A: I'll add something which I feel helps me understand this picture (since Alex has already done a nice job explaining things carefully). Let's do the simplest possible case where $S=\operatorname{spec}(k)$, for $k=\overline{k}$. You might even take $k=\Bbb{C}$. Then a morphism of affine schemes over $\operatorname{spec}(k)$ is a commutative diagram of schemes
$$
\require{AMScd}
\begin{CD}
\operatorname{spec}(A) @>{f}>> \operatorname{spec}(B)\\
@VVV @VVV \\
\operatorname{spec}(k) @>{=}>> \operatorname{spec}(k).
\end{CD}
$$
$\operatorname{spec}(A)$ and $\operatorname{spec}(B)$ are $k-$schemes so that their corresponding rings $A$ and $B$ are $k-$algebras. Then the dual diagram guarantees that the map $\varphi:B\to A$ corresponding to $f$ satisfies $\varphi\circ i=j$ where $i:k\to B$ and $j:k\to A$ are the structure maps of $k-$algebras. So, this just says that at the level of rings, our maps are nothing more than morphisms of $k-$algebras. Note that this condition $\varphi\circ i=j$ is necessary and sufficient for $\varphi$ to be a morphism of $k-$algebras.
A result (ChI section 1 exercises of Hartshorne) shows that morphisms of affine varieties $f:X\to Y$ over $k$ correspond to morphisms $\varphi:A(Y)\to A(X)$ of their affine coordinate rings viewed as $k-$algebras. So, if we want to take our affine variety $X$ and view it as a scheme, we take $\operatorname{spec}(A(X))$ and similarly for $Y$. Then the condition that $f$ be a morphism of schemes over $k$ corresponds in the affine case to the notion that specifying a morphism of varieties over $k$ is equivalent to specifying the map on their coordinate rings.
In general, you can think of a scheme over $k$ as being a gluing together of the spectra of various $k-$algebras so that the intuition basically works.
I'll briefly mention the case of $S$ being a more general scheme. In this case, we want to think of $X\to S$ as being a family of schemes $X_s$ parametrized by points $s\in S$. More concretely, if we have $X\to \Bbb{A}^1_k$, we might think of this as being some sort of family of $k-$schemes parametrized by the points of the affine line. Then the condition
$$
\require{AMScd}
\begin{CD}
X@>{f}>> Y\\
@VVV @VVV \\
\Bbb{A}^1_k @>{=}>> \Bbb{A}^1_k.
\end{CD}
$$
means (essentially) that the morphism is fibre preserving.
