Since $Y$ is affine we have $(Y, \mathcal{O}_Y) \cong (\Spec(R), \mathcal{O}_{\Spec(R)})$ for some ring $R$. During the proof we will use facts about $Y$ and its structure sheaf which are direct consequences of things we know about the spectrum of a ring, see e.g.\ Lemma \ref{lemma-spec-sheaves}. \medskip\noindent Motivated by the lemmas above we construct the inverse map. Let $\psi_Y : \Gamma(Y, \mathcal{O}_Y) \to \Gamma(X, \mathcal{O}_X)$ be a ring map. First, we define the corresponding map of spaces $$ \Psi : X \longrightarrow Y $$ by the rule of Lemma \ref{lemma-morphism-into-affine-where-point-goes}. In other words, given $x \in X$ we define $\Psi(x)$ to be the point of $Y$ corresponding to the prime in $\Gamma(Y, \mathcal{O}_Y)$ which is the inverse image of $\mathfrak m_x$ under the composition $ \Gamma(Y, \mathcal{O}_Y) \xrightarrow{\psi_Y} \Gamma(X, \mathcal{O}_X) \to \mathcal{O}_{X, x} $. \medskip\noindent We claim that the map $\Psi : X \to Y$ is continuous. The standard opens $D(g)$, for $g \in \Gamma(Y, \mathcal{O}_Y)$ are a basis for the topology of $Y$. Thus it suffices to prove that $\Psi^{-1}(D(g))$ is open. By construction of $\Psi$ the inverse image $\Psi^{-1}(D(g))$ is exactly the set $D(\psi_Y(g)) \subset X$ which is open by Lemma \ref{lemma-f-open}. Hence $\Psi$ is continuous. \medskip\noindent Next we construct a $\Psi$-map of sheaves from $\mathcal{O}_Y$ to $\mathcal{O}_X$. By Sheaves, Lemma \ref{sheaves-lemma-f-map-basis-below-structures} it suffices to define ring maps $\psi_{D(g)} : \Gamma(D(g), \mathcal{O}_Y) \to \Gamma(\Psi^{-1}(D(g)), \mathcal{O}_X)$ compatible with restriction maps. We have a canonical isomorphism $\Gamma(D(g), \mathcal{O}_Y) = \Gamma(Y, \mathcal{O}_Y)_g$, because $Y$ is an affine scheme. Because $\psi_Y(g)$ is invertible on $D(\psi_Y(g))$ we see that there is a canonical map $$ \Gamma(Y, \mathcal{O}_Y)_g \longrightarrow \Gamma(\Psi^{-1}(D(g)), \mathcal{O}_X) = \Gamma(D(\psi_Y(g)), \mathcal{O}_X) $$ extending the map $\psi_Y$ by the universal property of localization. Note that there is no choice but to take the canonical map here! And we take this, combined with the canonical identification $\Gamma(D(g), \mathcal{O}_Y) = \Gamma(Y, \mathcal{O}_Y)_g$, to be $\psi_{D(g)}$. This is compatible with localization since the restriction mapping on the affine schemes are defined in terms of the universal properties of localization also, see Lemmas \ref{lemma-spec-sheaves} and \ref{lemma-standard-open}. \medskip\noindent Thus we have defined a morphism of ringed spaces $(\Psi, \psi) : (X, \mathcal{O}_X) \to (Y, \mathcal{O}_Y)$ recovering $\psi_Y$ on global sections. To see that it is a morphism of locally ringed spaces we have to show that the induced maps on local rings $$ \psi_x : \mathcal{O}_{Y, \Psi(x)} \longrightarrow \mathcal{O}_{X, x} $$ are local. This follows immediately from the commutative diagram of the proof of Lemma \ref{lemma-morphism-into-affine-where-point-goes} and the definition of $\Psi$. \medskip\noindent Finally, we have to show that the constructions $(\Psi, \psi) \mapsto \psi_Y$ and the construction $\psi_Y \mapsto (\Psi, \psi)$ are inverse to each other. Clearly, $\psi_Y \mapsto (\Psi, \psi) \mapsto \psi_Y$. Hence the only thing to prove is that given $\psi_Y$ there is at most one pair $(\Psi, \psi)$ giving rise to it. The uniqueness of $\Psi$ was shown in Lemma \ref{lemma-morphism-into-affine-where-point-goes} and given the uniqueness of $\Psi$ the uniqueness of the map $\psi$ was pointed out during the course of the proof above.