In Hartshorne the fibred product of two schemes $X, Y$ over $S$ is defined to be $X \times_S Y$ together with morphisms $p_1: X \times_s Y \to X$,$p_2:X \times_s Y \to Y$ satisfying the universal property: Let $Z$ be any scheme over $S$, and given morphisms $f: Z \to X$ and $g: Z \to Y$ which make a commutative diagram with the given morphisms $X \to S$ and $Y \to S$, then there exists a unique morphism $\theta: Z \to X \times_S Y$ such that $f = p_1 \theta$ and $g = p_2 \theta$.
I was wondering is it necessary to state $Z$ to be a scheme over $S$ here? Instead of just any scheme $Z$ such that the maps $Z \to X \to S$ and $Z \to Y \to S$ are the same? ie Is the additional map from $Z \to S$, by saying $Z$ is over $S$, doing anything? Thank you.
No, it does not matter that $Z$ is a scheme over $S$. To be clear, in the definition, the morphisms $f$ and $g$ (and $\theta$) are supposed to be morphisms of schemes over $S$, meaning that the compositions $Z\to X\to S$ and $Z\to Y\to S$ are equal to the given morphism $Z\to S$. So, that given morphism is extraneous data that is uniquely determined by the rest of the setup. Similarly, given that $f$ and $g$ are morphisms over $S$, then $\theta$, if it exists, is automatically a morphism over $S$, because the $X\times_S Y$ is made into a scheme over $S$ using either $p_1$ or $p_2$ and so in particular the composition $Z\to X\times_S Y\to S$ must be equal to $Z\to X\times_S Y\to X\to S$ which is then equal to $Z\to X\to S$ which is equal to $Z\to S$ as before.
Here Hartshorne has really mixed up two different standard categorical definitions: the definition of a fiber product, or the definition of a product. You can define $X\times_S Y$ either as a fiber product in the category of schemes (in which case you wouldn't explicitly ask for $Z$ to be a scheme over $S$) or as a product in the category of schemes over $S$ (in which case everything is in the category of schemes over $S$ but you don't explicitly ask for the diagram of maps to $S$ to commute because that's part of the definition of a morphism of schemes over $S$). Hartshorne has taken pieces from each of these definitions and has ended up with a bit of confusing redundancy in his definition.