In Matsumura textbook, there is this following statement.
A ring homomorphism $f:A \to B$, induces a map $f': \operatorname{Spec}B \to\operatorname{Spec}A$ under which an element $\mathfrak{p} \in \operatorname{Spec} A$ has the inverse image ${f'}^{-1}(\mathfrak{p}) = \{P \in\operatorname{Spec} B: P \cap A = \mathfrak{p}\}$.
I am not able to understand how an ideal $P \subseteq B$ can be assumed to intersect with $A$.
It is a convention. For $P \in \mathrm{Spec}\ B$, the ideal $P\cap A:= f^{-1} (P)$, is a a point of $\mathrm{Spec}\ A$ (page 25 of the book).