EDIT: I want to add that the relevant parts of EGA to compare are [EGAI, Thm. 1.7.3], which is the analogue of [Hartshorne, II, Prop. 2.3(c)], and [EGAI, Prop. 2.2.4], which is the analogue of your exercise. This proof is similar to the other answer.
[EGAInew, Prop. 1.6.3] is what I am paraphrasing below. It is also [EGAII, Err$_\mathrm{I}$, Prop. 1.8.1], with attribution to Tate.
I won't write down all of the details, but here is another way to approach the problem, which I think is easier, since it avoids the issue with trying to cover $X$ by open affines and glueing morphisms together. We use that the category of schemes is a full subcategory of the category of locally ringed spaces. It suffices to show
\begin{align*}
\alpha\colon \operatorname{Hom}_\mathsf{LRS}(X,\operatorname{Spec} A) &\longrightarrow \operatorname{Hom}_\mathsf{Ring}(A,\Gamma(X,\mathcal{O}_X))\\
(f,f^\#) &\longmapsto f^\#(\operatorname{Spec} A)
\end{align*}
is bijective. We construct an inverse map
$$
\rho\colon \operatorname{Hom}_\mathsf{Ring}(A,\Gamma(X,\mathcal{O}_X)) \longrightarrow \operatorname{Hom}_\mathsf{LRS}(X,\operatorname{Spec} A)
$$
as follows. Let $\varphi\colon A \to \Gamma(X,\mathcal{O}_X)$ be given. Define
$$
f \colon X \to \operatorname{Spec} A, \quad x \mapsto \{s \in A \mid \varphi(s)_x \in \mathfrak{m}_x\}
$$
where $\varphi(s)_x$ is the image of $\varphi(s)$ in the stalk $\mathcal{O}_{x,X}$ and $\mathfrak{m}_x \subseteq \mathcal{O}_{x,X}$ is the maximal ideal of $\mathcal{O}_{x,X}$. Note the set on the right is a prime ideal. The map $f$ is continuous since $f^{-1}(D(r)) = \{x \in X \mid \varphi(r)_x \notin \mathfrak{m}_x\} = D(\varphi(r))$. We define the map $f^\#$ of structure sheaves; since $D(r)$ form a basis of $\operatorname{Spec} A$, we construct the morphism on each $D(r)$ and then glue. We define $f^\#(D(r))$ to be the top arrow in the diagram
$$
\require{AMScd}
\begin{CD}
A_r @>f^\#(D(r))>\exists!> \mathcal{O}_X(f^{-1}(D(r)))\\
@AAA @AAA\\
A @>\varphi>> \mathcal{O}_X(X)
\end{CD}
$$
induced by the the universal property of localization [Atiyah-Macdonald, Prop. 3.1], where the hypotheses for the universal property hold since $\varphi(r)$ is invertible in $\mathcal{O}_X(f^{-1}(D(r)))$ by definition of $f$. The morphisms on each $D(r)$ glue together since the maps $f^\#(D(r))$ were constructed uniquely by the universal property above, hence on any intersection $D(rs)$ they must match.
To show $\alpha$ and $\rho$ are inverse to each other, note $\alpha \circ \rho = \mathrm{id}$ is clear by letting $r = 1$ in the diagram above. This implies $\alpha$ is surjective, so it remains to show $\alpha$ is injective. Let $\varphi\colon A \to \Gamma(X,\mathcal{O}_X)$, and let $(f,f^\#)$ such that $\alpha(f,f^\#) = \varphi$. Then, we have the diagram
$$
\begin{CD}
A_{f(x)} @>f^\#_x>> \mathcal{O}_{x,X}\\
@AAA @AAA\\
A @>\varphi>> \mathcal{O}_X(X)
\end{CD}
$$
by taking the direct limit over all open sets $D(r)$ containing a point $x$. Since the map $f_x^\#$ is local, we have $(f_x^\#)^{-1}(\mathfrak{m}_x) = \mathfrak{m}_{f(x)}$, hence $f(x) = \{s \in A \mid \varphi(s)_x \in \mathfrak{m}_x\}$ as desired by using the commutativity of the diagram. The uniqueness of $f^\#$ also follows from this diagram since if $(g,g^\#)$ is any other map $X \to \operatorname{Spec}A$ such that $\alpha(g,g^\#) = \varphi$, then $f^\#_x = g^\#_x$ for all $x$, hence they must be the same morphism.
Let $X$ be a scheme an let $R$ be a ring. Let me maybe suggest you to try to prove the following:
There is a natural bijection $\text{Hom}_{\text{Sch}}(X, \text{Spec}(R)) \cong \text{Hom}_{\text{Ring}}(R, \Gamma(X,\mathcal{O}_X)).$
This is not only a very useful statement, but also implies what you want to prove as you can use that $\mathbb{Z}$ is a initial object in the category of rings like you were doing for the affine case.
Best Answer
I want to respond to one particular statement you made, but first I'll make things a bit more precise.
We have the adjunction $$\newcommand\Hom{\operatorname{Hom}}\newcommand\Spec{\operatorname{Spec}}\newcommand\calO{\mathcal{O}}\Hom(X,\Spec A)\simeq \Hom(A,\Gamma(X,\calO_X)),$$ and in particular this tells us that $$\Hom(X,\Spec \Gamma(X,\calO_X))\simeq \Hom(\Gamma(X,\calO_X),\Gamma(X,\calO_X)). $$
The identity map $\Gamma(X,\calO_X)\to\Gamma(X,\calO_X)$ therefore gives us a map of schemes $X\to \Gamma(X,\calO_X)$ as you noticed, and this is indeed the canonical map. However, you seem to be under the impression that this map is therefore an isomorphism.
In general this cannot possibly be true, since if the map were an isomorphism, $X$ would necessarily have to be affine. However, if $X$ is affine, this map is indeed an isomorphism.
Let's be a little more clear how this map works then.
In fact let's be a little more clear how it works in general. Let $\phi : A\to \Gamma(X,\calO_X)$ be a ring morphism. Let's try to understand the induced map $f : X\to \Spec A$.
Let $U$ be an affine open in $X$. Then we have the maps $$\newcommand\toby\xrightarrow A\toby{\phi}\calO_X(X)\toby{r_{XU}} \calO_X(U).$$ Taking $\Spec$ of this sequence gives $$U\toby{\Spec r_{XU}} \Spec \calO_X(X) \toby{\Spec \phi} \Spec A.$$ Gluing these maps together gives the desired map from $X$ to $\Spec A$.
Observe then that if $\phi=\newcommand\id{\operatorname{id}}\id$, that the map $X\to \Spec \Gamma(X,\calO_X)$ is the result of gluing the maps obtained from applying the Spec functor to the restrictions $r_{XU}:\calO_X(X)\to \calO_X(U)$.
If $X$ is affine, then we can take $U=X$, and there's no need to glue, the map $X\to \Spec\Gamma(X,\calO_X)$ is $\Spec \id=\id$. On the other hand, if $X$ is not affine, for example, if $X$ is a projective $k$-scheme with $k$ algebraically closed, then $\Gamma(X,\calO_X)=k$, and $X\to \Spec\Gamma(X,\calO_X)$ is the $k$-scheme structure morphism $X\to \Spec k$, which in general is clearly not an isomorphism.