Closed immersions are stable under base change My question can be summarized as:

I want to prove that closed immersions are stable under base change.

This is exercise II.3.11.a in Hartshorne's Algebraic Geometry. I researched this for about half a day. I consulted a number of books and online notes, but I found the proofs to be vague. Vakil's notes (9.2.1) hint that this is an immediate consequence of the canonical isomorphism $ M/IM\cong M\otimes_{A}A/I $, and so does Liu's book (1.23). The proof can't be this simple. I need to show that this can be reduced to the affine case, and to do so, I need to show that a closed immersion into an affine scheme is affine. I haven't been able to do so yet. Edit: I found a proof for this later but I still don't know how to use it to prove the question above.
As for the Stacks project, I had to traverse a tree propositions, until I eventually found a proof that uses concepts like quasi-coherent sheaves, something not introduced in Hartshorne's book at this point.
I also consulted Gortz & Wedhorn. The book cites section 4.11 as a proof for this. However the section is a general introduction to the categorical fibred product. It's unrelated. In fact the official errata mentions this error and cites proposition 4.20 instead. It is unclear to me how the proposition shows the result. I suspect the book uses a different - but equivalent - definition.
At this point, I'm frustrated. I'm self-studying and don't have anybody to ask. Could somebody please be kind enough to show me a self-contained proof?
 A: This is not an optimal solution, but if you didn't know that closed immersions can be checked affine locally (like I didn't), then this would be something you can do: check each condition for a closed immersion separately.
Let $X = \operatorname{Spec} R$, $X' = \operatorname{Spec} A$, and $Y = \operatorname{Spec} B$ be affine. Then, we know that in the diagram
$$\require{AMScd}
\begin{CD}
B \otimes_R A @<<< A\\
@AAA @AAA \\
B @<<< R
\end{CD}$$
$R \to B$ being surjective implies $A \to B \otimes_R A$ is surjective by the right exactness of the tensor product.
Now what we want to do is to use the same idea to show the surjectivity of the map $f^{\prime\#}$ on structure sheaves, and to do so you can check surjectivity on stalks. Thus, by using small enough open neighborhoods you can assume $X,X',Y$ are affine and using the notation above, you want to show $A_P \to (B \otimes_R A)_P$ is surjective for each $P \in \operatorname{Spec} A$, where you consider $B \otimes_R A$ as an $A$-module. Now the thing was that I decided to localize the diagram above at the prime $P$: you're right that I was wrong to put my prime $P$ in $\operatorname{Spec} R$ instead of $\operatorname{Spec} A$. 
Instead, you can get the same conclusion by noticing $(B \otimes_R A)_P = B \otimes_R A \otimes_A A_P = B \otimes_R A_P$, so we want to show $A_P \to B \otimes_R A_P$ is surjective. But $R \to B$ is surjective since it is locally surjective by assumption that $Y \to X$ is a closed immersion, so right exactness works again.
Now, the question is how you get the topological part of being a closed immersion. The proof above combined with Exercise 2.18(c) shows we have a homeomorphism with a closed subset of $X'$ when assuming $X,X',Y$ are affine. You can then play the whole gluing game like in the construction of the fiber product to deduce that for arbitrary $X,X',Y$ you have this topological property.
In hindsight, this is not a good way to go!
EDIT: I was requested to flesh out the topological part of the proof. Recall the setup: want to show that in the diagram
$$\begin{CD}
Y \times_X X' @>f'>> X'\\
@VVV @VV{g}V \\
Y @>f>> X
\end{CD}$$
that if $f$ is a closed immersion, then $f'$ is a closed immersion. The proof above shows that $f^{\prime\#}$ is indeed surjective at each stalk, and so we must show $f'$ induces a homeomorphism between $Y \times_X X'$ and a closed subset of $X'$. I think the hard part is proving we can reduce to the case when $X,X',Y$ are affine, so I will be doing this below. We follow EGAI, Cor. 4.2.4 and Prop. 4.3.1. Note that the new edition has another proof.
We first note that closed immersions are local on target. The property on stalks is trivially local on target; the property "a morphism $f\colon Y \to X$ induces a homeomorphism between $Y$ and a closed subset of $X$" is local on target as well. For, suppose $\{X_\lambda\}$ is a cover of $X$ and $Y_\lambda := f^{-1}(X_\lambda)$, such that $Y_\lambda \to X_\lambda$ induces a homeomorphism between $Y_\lambda$ and $f(Y_\lambda)$ a closed subset of $X_\lambda$. By hypothesis, if $y \in Y$, every neighborhood of $y$ is mapped to a neighborhood of $f(y)$, and $f$ is injective. So, it remains to show that $f(Y)$ is actually closed in $X$. But it suffices to show $f(Y) \cap X_\lambda$ is closed in $X_\lambda$ for every $\lambda$; but this is trivial since $f(Y) \cap X_\lambda = f(Y_\lambda)$ is closed in $X_\lambda$.
Now back to the claim at hand. We first reduce to the case where $X$ is affine. First let $\{X_\lambda\}$ be an affine cover of $X$, and let $Y_\lambda := f^{-1}(X_\lambda)$ and $X'_\lambda := g^{-1}(X_\lambda)$. The restriction $f\rvert_{Y_\lambda}\colon Y_\lambda \to X_\lambda$ is closed immersion, hence if the proposition holds for $X$ affine, then $Y_\lambda \times_{X_\lambda} X'_\lambda \to X'_\lambda$ is a closed immersion. Now by Thm. 3.3, Step 7, the $Y_\lambda \times_{X_\lambda} X'_\lambda$ are canonically isomorphic to $Y \times_X X'_\lambda$; the morphism $Y_\lambda \times_{X_\lambda} X'_\lambda \to X'_\lambda$ is the same as the restriction of $f'$ to $Y \times_X X'_\lambda$ ; since the $X'_\lambda$ form a cover of $X'$, we have that $f'$ is a closed immersion assuming the proposition holds for $X$ affine by the fact that closed immersions are local on target.
Now we further reduce to the case where $X'$ is affine as well; note that $X$ affine implies $Y$ is affine since it is a closed subscheme of $X$ by Exercise 3.11(b). But if $\{X'_\mu\}$ is an affine open cover of $X'$, then $Y \times_X X'_\mu \to X'_\mu$ is a closed immersion by the affine case we wanted to reduce to, hence $Y \times_X X' \to X'$ is a closed immersion since closed immersions are local on target.
A: Fact: $X, Y$ are schemes over $S$, if $U\subset X$ is an open subset and if the product $X\times _S Y$ exists then $p_1 ^{-1}(U)= U\times _S Y$ .
Let $f:Z \rightarrow X$ is a closed immersion. equivalently there is an open affine covering $\{U_i\}$, $U_i=\operatorname{Spec}(A_i)$ of $X$, such that $f^{-1}(U_i)=\operatorname{Spec}(A_i/I_i)$ for some $I_i$ ideal of $A_i$.
Let $g: Y\rightarrow X$ is a morphism . To show $p_2: Z\times _X Y \rightarrow Y$ is a closed immersion.
$$\require{AMScd}
\begin{CD}
Z\times _X Y @>>> Y \\
@VVV @VVV \\
Z @>>> X
\end{CD}$$
We can choose an open affine covering $\{\operatorname{Spec}(B_i)\}$ of $Y$ such that $g(\operatorname{Spec}(B_i))\subset (\operatorname{Spec}(A_i))$ .
By the Fact above,  $p_2^{-1}(\operatorname{Spec}(B_i))= (\operatorname{Spec}(B_i))\times_X Z $
$= (\operatorname{Spec}(B_i))\times_{\operatorname{Spec}(A_i)} \operatorname{Spec}(A_i/I_i) $
( because $g(\operatorname{Spec}(B_i))\subset \operatorname{Spec}(A_i)$ and $f^{-1}(\operatorname{Spec}(A_i))= \operatorname{Spec}(A_i/I_i)$)
$=\operatorname{Spec}(B_i\otimes_{A_i} A_i/I_i)$
$=\operatorname{Spec}(B_i/I_i B_i)$   (becasue $M/IM\cong M\otimes_{A}A/I$)
Therefore $p_2$ is a closed immersion.
A: Consider a cartesian diagram 
$$\require{AMScd}
\begin{CD}
X^\prime @>>> X \\
@VVV @VVV \\
Y^\prime @>>> Y
\end{CD}$$
where $X\to Y$ is a closed immersion. Let's call $g$ the function $X'\to Y'$. 
We know that, taking $Y_i$ an open affine covering of $Y$, and  $X_i$, $Y_i'$ open affine covering of $X$ and $Y'$ such that $Y_i'\to Y_i$ and $X_i\to Y_i$ are open embeddings, then the fibered product $X'=X\times_Y Y'$ is covered by the open affine schemes $X_i\times_{Y_i}Y_i'$.
In particular, given the projections $X_i\times_{Y_i}Y_i'\to Y_i'$, they glue togheter, obtaining the morphism $g$. 
This means that we can test a property in the affine case
A: In the below, we consider a criterion for stability of base change that works in this exercise.
Proof of this criterion using abstract non-sense is given at the end.

Criterion for Stability under Base Change
Let $\mathbf{P}$ be a property that one can talk about on morphisms of schemes.
Suppose the following holds :

*

*Given $f:X\to Y$, for an open subset $U$ of $Y$, if $f$ has property $\mathbf{P}$, so does the base change of $f$ along the inclusion of the open subscheme $U\subset Y$.


*Given $f:X\to Y$, for an open cover $\{U_i\}_i$ of $Y$, if the base change of each $f$ along the inclusion of the open subscheme $U_i\subset Y$ has property $\mathbf{P}$, so does $f$.
then $\mathbf{P}$ is stable under base change iff $\mathbf{P}$ is stable under base change along morphisms with target and domain between affine schemes.
Solution
Closed immersions clearly satisfy condition 1., 2. : on the level of topological spaces, things are clear; on the level of sheaves, use stalks to check epimorphicity.
In Hartshorne's Exercise 3.11(b), closed immersions with target an affine scheme are characterized as the $\operatorname{Spec}$ of some $A\to A/\mathfrak{a}$). Therefore, after verifying condition 1., 2. in the above, we have reduced the problem to the computation of the pushout of some span $A\leftarrow B\rightarrow B/I$ (which gives $A\to A/IA$).

Proof of Criterion
Consider $Z\rightarrow X\leftarrow Y$ with pullback $W$, where $Y\rightarrow X$ has property $\mathbf{P}$.

*

*The case $X$ is affine : choose an open affine cover $\{Z_i\}_i$ of $Z$, note that

$$(W\times_{Z}Z_i\to Z_i)\simeq (Y\times_XZ_i\to Z_i)$$
As $X,Z_i$ are affine, condition 2. shows $W\to Z$ has property $\mathbf{P}$.

*

*The general case : choose an affine cover $\{X_i\}_i$ of $X$, let $Z_i=X_i\times_XZ$, we have

$$(W\times_{Z_i}Z)\simeq(Y\times_XX_i)\times_{X_i}Z_i$$
By condition 1., $(Y\times_XX_i)\to X_i$ has property $\mathbf{P}$, and as $X_i$ is affine, this concludes the proof of the criterion.
