I am having trouble understanding the last line in the proof of this proposition. I have seen this question once on Math.SE and another time in MO, but I couldn't find in either case a clear solution to my doubt. Here is the situation and the claim that Hartshorne does:
Let $f\colon X\to Y$ be a morphism between $X$ a complete nonsingular curve and $Y$ any curve such that $f(X)=Y$. We know then that $Y$ is also complete (but may be singular) and that $f$ induces a finite field extension $K(Y)\subseteq K(X)$ between the fraction fields. Goal: $f$ is a finite morphism, i.e. the preimage of an affine open $\operatorname{Spec}{B}$ is an affine open $\operatorname{Spec}{A}$ such that the induced ring morphism makes $A$ a finite $B$-module.
So let $V=\operatorname{Spec}{B}$ be an affine open of $Y$ (potentially with some singularities). Regard $B$ as a subring of $K(X)$ (via the identification with $f$) and let $A$ be the integral closure of $B$ in $K(X)$. Then $A$ is a Dedekind domain, finite $B$-module, with fraction field $K(X)$. $U=\operatorname{Spec}{A}$ is a nonsingular curve with fraction field $K(X)$, hence canonically an open subscheme of $X$ (because complete nonsingular curves are canonically isomorphic to the abstract complete nonsingular curve, whose closed points are the dvr's in the corresponding field). Hartshorne claims then that clearly $f^{-1}(V)=U$, but I fail to see why, let alone why this is at all so clear.
My attempt: the valuation rings of a field containing a Dedekind domain are precisely the localization of this Dedekind domain at the different non-zero prime ideals. Hence a closed point $P\in X$ is a non-zero prime ideal of $A$ if and only if $\mathcal{O}_{X,P}\supseteq A$, and since $A$ is the integral closure of $B$ in $K(X)$, this is the case if and only if $\mathcal{O}_{X,P}\supseteq B$. But this means precisely that the prime $P$ lies over some prime of $B$ under the identification done via $f$, hence that $P\in f^{-1}(V)$.
Is this proof correct? It is far too handwavy for my taste, but I don't know how to write a more formal proof. What would be the right way to write down a complete formal proof of this?
Edit: (in an attempt to make my doubt more precise)
Why is the image under $f$ of the isomorphic image of $\operatorname{Spec}{A}$ in $X$ contained in $\operatorname{Spec}{B}$? And why is the restriction of $f$ to this isomorphic image the same as the map induced by the inclusion of rigns?
I suspect this to be true because we are using $f$ to identify $B$ as a subring of $K(X)$, but I fail to see how to write down an easy formal proof of this. Moreover, since Hartshorne writes "clearly", I am afraid that the answer is actually easy and it may follow directly from some of the things I wrote above. But I don't see how.
Any help is appreciated, thanks.
As an attempt to assuage your concerns about "under the identification done via $f$", I present the following lemma:
Lemma: A dominant morphism $f:X\to Y$ induces an injection on coordinate algebras $f^*:\mathcal{O}_{Y}(Y)\to \mathcal{O}_X{X}$.
Proof: Suppose $c\in \mathcal{O}_{Y}(Y)$ is in the kernel of $f^*$. Then $c$ must vanish on $f(X)$. But if $f$ is dominant, then $f(X)$ is dense, so $c$ must be the zero function.
How do we use this to eliminate the doubts you have? Set $\operatorname{Spec} B = V\subset Y$ an affine open subset. Then $f^{-1}(V)$ is an open subset of $X$, and furthermore, by the lemma above, $B \hookrightarrow K[f^{-1}V]$ which embeds into $K(X)$. So we have a morphism $B\hookrightarrow K(X)$ which is an injection. This gives you $B$ as a subring of $K(X)$ in a natural way and it would be compatible with anything you would ever want.
Responding to edit made 10/03: The question has been clarified to ask why $f(\operatorname{Spec} A)\subset \operatorname{Spec} B$.
First: why does $f$ match the map induced by ring inclusions? Because the ring inclusion $A\subset B$ is performed via $f$. Tracing the definitions, the ring map $A\to K(Y)\to K(X)$ is performed by the natural embedding of a domain into it's field of fractions and then sending a rational function in $K(Y)$ to a rational function in $K(X)$ by pulling back via $f$.
So $f$ is the map on spectrums induced by the ring inclusion $i:B\to A$. Since for any morphism of affine schemes induced by a ring map we have that the image of a prime ideal on the scheme side is it's preimage on the ring side, we have that for $\mathfrak{p}\in \operatorname{Spec} A$, $f(\mathfrak{p})= B\cap\mathfrak{p}$, which is an ideal of $B$. Furthermore, there are no ideals of $A$ which have trivial intersection with $B$: given an element $a\in A\setminus B$, if an ideal contains $a$, then the ideal contains all powers of $a$, and the monic polynomial in $B$ which vanishes on $a$ (which exists because $A$ is an integral extension of $B$) enables us to write a $B$-linear combination of powers of $a$ as an element of $B$. This shows that $f(\operatorname{Spec} A)\subset \operatorname{Spec} B$.