If you want to prove the statement in question using a gluing argument, then you need to prove the result in the special case that $X=\mathrm{Spec}(B)$ is also affine. This will allow you to conclude that your maps on intersections of affine opens agree on overlaps (because $\mathrm{Hom}(\mathrm{Spec}(B),\mathrm{Spec}(A))\rightarrow\mathrm{Hom}(A,B)$ is a bijection).
The result for $X=\mathrm{Spec}(B)$ is proved in Hartshorne if I remember correctly. In any case, the idea is that a ring map $\varphi:A\rightarrow B$ induces a continuous map $\alpha:\mathrm{Spec}(B)\rightarrow\mathrm{Spec}(A)$ under which the inverse image of a standard open $D(f)\subseteq\mathrm{Spec}(A)$ is $D(\varphi(f))$ ($f\in A$). The homomorphism $\varphi$ naturally induces, for each $f\in A$, a ring map $A_f\rightarrow B_{\varphi(f)}$, i.e., a map $\mathcal{O}_{\mathrm{Spec}(A)}(D(f))\rightarrow\mathcal{O}_{\mathrm{Spec}(B)}(D(\varphi(f))$, compatible with restriction, and since the sets $D(f)$ give a base for the topology of $\mathrm{Spec}(A)$, this is extends uniquely to a sheaf map $\mathcal{O}_{\mathrm{Spec}(A)}\rightarrow\alpha_*\mathcal{O}_{\mathrm{Spec}(B)}$. For $\mathfrak{q}\in\mathrm{Spec}(B)$, $\mathfrak{p}=\alpha(\mathfrak{q})=\varphi^{-1}(\mathfrak{q})$, the stalk map $A_\mathfrak{p}\rightarrow B_\mathfrak{q}$ is also induced (via the universal property of localization) by $\varphi$, and is easily seen to be local (by construction). This morphism $\alpha$ also recovers $\varphi$ on global sections.
Uniqueness follows ultimately because of the requirement that the stalk maps of a morphism of locally ringed spaces are local and because morphisms of sheaves are determined by the morphisms on stalks. The requirement that the stalk maps be local forces $\alpha(\mathfrak{q})=\varphi^{-1}(\mathfrak{q})$, and then, again using the universal property of localization, there is a unique map of stalks $A_{\varphi^{-1}(\mathfrak{q})}\rightarrow B_\mathfrak{q}$ compatible with $\varphi$. In summary, if you want to recover $\varphi$ on global sections and you want your stalk maps to be local, you only have one choice (everything is induced by the universal property of localization).
I want to point out that this argument actually extends to prove the result you're after with $X$ replaced not just by an arbitrary scheme, but by an arbitrary locally ringed space. This means the result actually has nothing to do with gluing (since an arbitrary locally ringed space needn't be built from affine schemes). The crucial thing is (as mentioned above) that for a morphism $\alpha:X\rightarrow\mathrm{Spec}(A)$ the stalk map $\alpha_x^\sharp:A_{f(x)}\rightarrow\mathcal{O}_{X,x}$ is local for any $x\in X$. Note that here $f(x)$ is a prime ideal of $A$ and $A_{f(x)}$ is the localization at that prime (the stalk of the structure sheaf of $\mathrm{Spec}(A)$ at $f(x)$). If $\alpha^\sharp:A\rightarrow\mathcal{O}_X(X)$ is the map on global sections of the morphism $\alpha$, then its compatibility with the stalk map $\alpha_x^\sharp$ and the fact that $\alpha_x^\sharp$ is local actually implies that $f(x)$ is the inverse image of the maximal ideal $\mathfrak{m}_x\subseteq\mathcal{O}_{X,x}$ under the ring map $A\rightarrow\mathcal{O}_X\rightarrow\mathcal{O}_{X,x}$ (the first arrow is $\alpha^\sharp$ and the second is taking the stalk at $x$). So the map on global sections determines the morphism $\alpha$ on the underlying topological spaces. Once you know this, it also follows that the stalk maps are uniquely determined.
The point of all this is that the map you want to prove is a bijection is injective. To prove that it is surjective, you basically run the above argument backwards. Given a ring map $\varphi:A\rightarrow\mathcal{O}_X(X)$, you can define $\alpha:X\rightarrow\mathrm{Spec}(A)$ on topological spaces by taking $f(x)$ to the prime ideal that is the inverse image of $\mathfrak{m}_x\subseteq\mathcal{O}_{X,x}$ under the map mentioned in the previous paragraph. You can prove then that for any $f\in A$, $\alpha^{-1}(D(f))$ is $X_{\varphi(f)}$, defined as the set of all $x\in X$ such that $\varphi(f)_x$ is not in the maximal ideal $\mathfrak{m}_x$ of $\mathcal{O}_{X,x}$. This is an open set of $X$ (the analogue of a standard open in an affine scheme), so $\alpha$ is continuous. The universal property of localization together with the fact that $\varphi(f)\vert_{X_{\varphi(f)}}\in\mathcal{O}_X(X_{\varphi(f)})$ is a unit (this follows from the definition of $X_{\varphi(f)}$) shows that the ring map $A\rightarrow\mathcal{O}_X(X)\rightarrow\mathcal{O}_X(X_{\varphi(f)})$ ($\varphi$ followed by restriction from $X$ to $X_{\varphi(f)})$ induces a unique map $A_f\rightarrow\mathcal{O}_X(X_f)$ compatible with restriction. This data is what you need for a map of sheaves $\mathcal{O}_{\mathrm{Spec}(A)}\rightarrow\alpha_*\mathcal{O}_X$. This $\alpha$ recovers $\varphi$ on global sections and proves surjectivity.
Just to explain, the reason I went through all this was to illustrate that the adjunction between the global sections functor and $\mathrm{Spec}$ actually works on the entire category of locally ringed spaces (not just the full subcategory of schemes) and so really has nothing to do with gluing maps on affine patches. I personally (when learning algebraic geometry) found this fact really helped me and shaped the way I think of affine schemes among locally ringed spaces (or just schemes). For example, any locally ringed space $X$ admits a unique morphism to $\mathrm{Spec}(\mathbb{Z})$, and the method of proof I've described shows that it sends a point $x\in X$ to the prime ideal generated by the characteristic of the residue field $ \kappa(x)$.
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.
Best Answer
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.