Question regarding proof in Hartshorne on existence of the fibered product Let $X$ and $Y$ be schemes over $S$. For the time being let us assume that $X$, $Y$, and $S$ are affine schemes such that $X= Spec \hspace{0.5mm} A$, $Y = Spec \hspace{0.5mm} B$, and $S= Spec \hspace{0.5mm} R$. 
Since $X$ and $Y$ are schemes over $S$ there exists morphisms: 
$$ Spec \hspace{0.5mm} A \to Spec \hspace{0.5mm} R$$
$$ Spec \hspace{0.5mm} B \to Spec \hspace{0.5mm} R$$
Hartshorne then claims that $A$ and $B$ are $R$-algebras. 
Why are $A$ and $B$ necessarily $R$-algebras? How does the existence of a morphism imply this? 
 A: This is due to the fact that the category of affine schemes is the opposite category to the category of rings. $Hom_{Sch}(Spec(A),Spec(R))=Hom(R,A)$.
A: The geometric intuition behind a map $Spec A \to Spec B$ is that functions on Spec B, namely the elements of the ring $B$, are pulled back to functions on the space $Spec A$, which is the ring A. So this is the same as a ring morphism from $B$ to $A$. On the other hand, any ring morphism from $B$ to $A$ pulls back the coordinate functions on $Spec B$ to functions on $A$, so in principle we know where each "point" goes. (This makes the most sense for the theory of varieties over $\bar{k}$.)
But one needs to check that the formalism set up in Hartshorne describes this intuition correctly.
So $Spec A$ is the prime spectrum, endowed with a certain sheaf of rings $O_A$. It is most convenient to consider this sheaf on the basis of open sets $D(f)$, the set of primes not containing $f \in A$, where the ring is $A[f^{-1}]$. The reason is that the sections over these opens are easily described (previous sentance), and these are usually the only open sets one uses anyway. Some work has to be done to see that this uniquely defined a sheaf on the spectrum, but it is mostly formal. One can check that the stalks are local rings, the stalk at $p$ is $A_p$.
We consider only maps of locally ringed spaces, which means that the induced morphisms on the stalks send the maximal ideal into the maximal ideal - the geometric reason here is that if a function $f$ vanishes at a point $p$, the pullback along some $\pi : X \to Y$, $\pi^* f$, should vanish at all points $q \in \pi^{-1}(p)$). If you consider instead all maps of ringed spaces, you can get some weird stuff, which is not very geometric, and doesn't come from morphisms of rings.
So what you need to check:


*

*Any ring morphism from $\phi: A \to B$ induces (functorially) a morphism of locally ringed spaces from $Sec B$ to $Spec A$. You send a prime $p$ to $\pi^{-1}(p)$ to get a map $\pi$ on the underlying spaces, and check that this is continuous in the Zariski topology. Then you need to construct a map of sheaves $O_B$ to $\pi_* O_A$ on $Spec B$; this can be done over each of the distinguished opens, and compatibility and existence is expressed by the universal property of localization.

*Any morphism of locally ringed spaces between $Spec A$ and $Spec B$ is induced in the way described in 1. This is a little tricker, but of course a morphism $Spec A \to Spec B$ induces a map on global sections (which are $A$ and $B$, respectively), and you will want to show that this map is the one you started with. For this you need it to be morphism of locally ringed spaces.


So at the end you will have shown that $Spec$ is an equivalence of categories from Rings to Affine Schemes (with morphisms maps of locally ringed spaces), with "quasi"inverse functor global sections. (It's just quasi because there are many choices of "inverse" to an equivalence of categories, but this one is the natural one.)
A good reference for these foundational questions are the excellent notes by Ravi Vakil.
