Let $\pi: X \to Y$ be a locally closed embedding of schemes. Then we can realize $X$ as a closed subscheme of an open subscheme $U$ of $Y$, and $\pi$ factors as $$X \to U \to Y$$ where $X \to U$ is a closed embedding and $U \to Y$ is an open embedding.
We want to show that $\pi$ is a closed embedding. Therefore, we need to show that for any open affine subset $\operatorname{Spec} B$ of $Y$, the preimage $\pi^{-1}(\operatorname{Spec} B)=X \cap \operatorname{Spec} B$ is affine, say $\operatorname{Spec} A$, and the induced map of sections $B \to A$ is surjective.
The problem I am seeing is that the open embedding $U \to Y$ need not be affine, i.e., $U \cap \operatorname{Spec} B$ need not be affine, and therefore $X \cap U \cap \operatorname{Spec} B= X\cap \operatorname{Spec} B$ need not be affine; but we need it to be.