I just want to correct one mistake in my counterexample, and I want to make another philosophical point. First, you do need some hypothesis such as quasi-coherence of $\mathcal{A}$. The counterexample I wrote is wrong; here is a correction. Let $Y$ be $\mathbb{A}^1_k = \text{Spec}\ k[t]$. Let $0$ be the closed point with corresponding maximal ideal $\langle t \rangle \subset k[t]$. Let $\mathcal{A}$ be the $\mathcal{O}_Y$-module such that $\mathcal{A}(U)$ is $\{0\} = k[t]/\langle 1 \rangle$ if $U$ does not contain $0$, and such that $\mathcal{A}(U) = k(t)$ as a $k[t]$-algebra if $U$ does contain $0$. For open subsets $V\subset U$, the restriction homomorphism $\mathcal{A}(U)\to\mathcal{A}(V)$ is either zero, if $V$ does not contain $0$, or the identity on $k(t)$ if $V$ does contain $0$. It is straightforward to check that this is a sheaf (this is the mistake in my previous example). For every open affine $U\subset Y$, there is a natural morphism $$f_U:\text{Spec}\ \mathcal{A}(U) \to U,$$ and for every pair of open affines $V\subset U \subset Y$, there is a natural commutative diagram, $$\begin{array}{ccc} \text{Spec}\ \mathcal{A}(V) & \xrightarrow{f_V} & V \\ \downarrow & & \downarrow \\ \text{Spec}\ \mathcal{A}(U) & \xrightarrow{f_U} & U \end{array}.$$ However, this commutative diagram is not a fiber product diagram: if $U$ contains $0$ but $V$ does not, the fiber product is $\text{Spec}\ k(t)$, but $\mathcal{A}(V)$ is the zero ring. Therefore, there is no morphism $f:X\to Y$ and collection of isomorphisms $$\phi_U:f^{-1}(U) \xrightarrow{\cong} \text{Spec}\ \mathcal{A}(U),$$ as schemes over $U$. If there were, then $\text{Spec}\ \mathcal{A}(V)$ as above would be $f^{-1}(V)$, and this is the fiber product of $f^{-1}(U)\to U$ and $V\to U$.
Now here is the philosophical point. One way to "calibrate" the gluing is to write down a universal property satisfied by the glued object and that is compatible with restricting to open subsets. In this case, the universal property of $f:X\to Y$ is that there is a homomorphism of $\mathcal{O}_X$-algebras, $s:f^*\mathcal{A}\to \mathcal{O}_X$, and this is universal: for every $g:Z\to Y$ and homomorphism of $\mathcal{O}_Z$-algebras, $t:g^*\mathcal{A}\to \mathcal{O}_Z$, there exists a unique morphism $h:Z\to X$ such that $f\circ h$ equals $g$ and such that $t$ equals $h^*s:h^*f^*\mathcal{A}\to h^*\mathcal{O}_X$. In the quasi-coherent case, where locally $\mathcal{A}_U = \widetilde{A_U}$ for an $\mathcal{O}_Y(U)$-algebra $A_U$, it is straightforward to check that locally $\text{Spec}\ \mathcal{A}(U) \to U$ satisfies the universal property. Then, for open affines $U$ and $V$, the corresponding universal properties give an isomorphism of $f_U^{-1}(U\cap V)$ and $f_V^{-1}(U\cap V)$ as schemes over $U\cap V$. Finally, the uniqueness part of the universal property implies the cocycle condition for these isomorphisms.
Best Answer
An affine scheme can be characterized in the category of locally ringed spaces (one needs the "locally" if I remember correctly). A l.r.s. $X$ is an affine scheme i.f.f. $Hom(Y,X)$ functorially equals $Hom(\Gamma(X,\mathcal{O}_X),\Gamma(Y,\mathcal{O}_Y))$, for $Y$ a l.r.s.
In other words, the affine scheme construction is the construction of a right adjoint to $\Gamma: ( l.r.s. ) \to ( rings )^{op}$.