Let $\phi:A\to B$ be a ring morphism, and let $f:X=Spec(B)\to Y=Spec(A)$ be the induced map of affine schemes.
I'm trying to show that if $f$ is a homeomorphism onto a closed subset of $Y$ and $f^\#:\mathcal{O}_Y\to f_*(\mathcal{O}_X)$ is surjective, then $\phi$ is surjective.
Hartshorne suggests to factor the map through the quotient. So let $\pi:A\to A/ker \phi$ and $\tilde \phi:A/ker \phi\to B$ be the canonical maps.
$\tilde \phi$ induces a map $\tilde f:X\to Y'=Spec (A/ker\phi)$, and $\pi$ induces a map $g:Y'\to Y$.
My idea is: prove that $g_*\tilde f^\#$ is an isomorphism. Then, if we apply the global sections functor to it, we get $\tilde \phi$ which is then also an isomorphism.
First, $g_*\tilde f^\#$ is surjective, since $f^\#=g_*\tilde f^\# \circ g^\#$, and $f^\#$ is surjective by hypothesis.
Now, since $\tilde \phi$ is injective, we get get that $\tilde f^\#$ is injective (previous part of the exercise). The direct image functor $g_*$ is left exact (since it is the right adjoint to the inverse image functor $g^{-1}$), so $g_*\tilde f^\#$ is also injective.
Thus $g_*\tilde f^\#$ is an isomorphism.
Then $(g_*\tilde f^\#)_Y=\tilde f^\#_{Y'}=\tilde \phi$ is an isomorphism. QED
Something must be wrong with my argument since I didn't use the hypothesis that $f$ is an homeomorphism onto a closed subset of $Y$.
I've seen solutions online that do the following: prove that $\tilde f$ is an homeo, then somehow (not explicited) from surjectivity of $g_*\tilde f^\#$ we get surjectivity of $\tilde f^\#$. Then $\tilde f$ is an isomorphism of affine schemes, thus $\tilde \phi$ is an isomorphism of rings.
How do they deduce surjectivity of $\tilde f^\#$ from surjectivity of $g_*\tilde f^\#$?