Start with this theorem:
Let $f : X \rightarrow Y$ be continuous and injective. Let $(U_i)_{i \in I}$ be an open cover of $Y$. Suppose that $f|_{f^{-1}(U_i)} : f^{-1}(U_i) \rightarrow U_i$ are all homeomorphisms onto their range. Then, $f$ is a homeomorphism onto its range.
Proof: To check this, we need to show that the inverse is continuous (as a function $range(f) \rightarrow X$). Let $V \subseteq X$ be open, and we want to show $f^{-1}(V)$ is open in $range(f)$. It suffices to show that $V \cap U_i$ is open in $range(f)$ for each $i \in I$. This is true since $f|_{f^{-1}(U_i)}$ is a homeomorphism onto its range, and $U_i$ being open means that $V \cap U_i$ is open in $range(f)$ iff it's open in $range(f) \cap U_i$. $\blacksquare$
Now to schemes : let $f : X \rightarrow Y$ be a morphism between schemes, and let $p \in Y$ be a point. Let $V \subseteq Y$ be an affine open set containing $p$ and $U \subseteq X$ be an affine open set inside $f^{-1}(V)$. Then, make this diagram:

The two squares are fiber products, so the outside rectangle is also a fiber product. Since $f(U) \subseteq V$, $U \times_Y Spec(k(p)) = U \times_V Spec(k(p))$. This is a fiber product of affine schemes over an affine base, so (assume we already know the result for affine schemes) we know that $\pi_1^{-1}(U) \rightarrow U$ is a homeomorphism onto its range. Therefore, it's a local homeomorphism.
The last thing to check is to show that it's injective. I'll use theorem 26.17.5 in the Stacks Project for this. Let $x \in X$ with $f(x) = p$. The points in $X \times_Y Spec(k(p))$ whose image in $X$ is $x$ are in bijection with prime ideals in $k(x) \otimes_{k(p)} k(p) = k(x)$, but $k(x)$ is a field so the only prime ideal is $(0)$. Therefore, $\pi_1$ is injective.
Here is how to show the theorem for affine schemes:
First, note that if $\phi : A \rightarrow B$ is a ring homomorphism, and $I$ is an ideal of $A$, and $S$ is a multiplicatively closed subset of $A$ , then $A/I \otimes_A B = B / \phi(I)B$ and $S^{-1}A \otimes_A B = \phi(S)^{-1}B$. (I'll leave the proofs to you, the idea is to find a bilinear map by multiplication, apply the universal property of tensor products, and then find an inverse) .
Now, let $p \subseteq A$ be a prime ideal, and let $\phi' : A/p \rightarrow B / \phi(p) B$ be the induced ring homomorphism, and construct this diagram (all squares are tensor products):

Both localization and quotients induce morphisms on Spec that are homeomorphisms onto their range. $R \rightarrow R/I$ is a homeomorphism onto $V(I)$, and $R \rightarrow S^{-1}R$ is a homeomorphism onto the prime ideals disjoint from $S$.
In our case, $B \rightarrow \phi'(A/p - \{0\})^{-1} (B / \phi(p) B)$ induces a homeomorphism onto its range, and its range are the prime ideals $q$ of $B$ that contain $\phi(p)$ and the image of $q$ in $B / \phi(p)$ doesn't contain anything in $\phi'(A/p - \{0\})$. In other words, it doesn't contain anything in $range(\phi')$ other than 0.
Now, note that $q$ satisfies that iff $\phi^{-1}(q) = p$. I'll leave this verification to you as well.
Therefore, $B \rightarrow \phi'(A/p - \{0\})^{-1} (B / \phi(p) B)$ is a homeomorphism onto its range, and its range is precisely the prime ideals $q$ in $B$ such that $\phi^{-1}(q) = p$