Morphisms between affine schemes Suppose we have two affine schemes $X=\operatorname{Spec} A$ and $Y=\operatorname{Spec} B$ for commutative rings $A,B$. I encountered this statement in my homework that $\operatorname{Mor}(X,Y)=\operatorname{Hom}(B,A)$. I understand that a ring homomorphism between $B$ and $A$ would induce a continuous map between $X$ and $Y$. But isn't that only for the underlying topological space? Why we don't need to check it is indeed a morphism between the sheaves here?
More generally, if $X$ and $Y$ are general schemes, is it still enough to specify a continuous map between the underlying topological space to define a morphism between the two schemes?
 A: A map $B \longrightarrow A$ does not generally describe a homeomorphism $\operatorname{Spec} A \longrightarrow \operatorname{Spec} B$, but it does describe a continuous map. For instance, the map $\mathbb Z \longrightarrow \mathbb Z/2$ corresponds to the inclusion $\{(2)\} \subseteq \operatorname{Spec} \mathbb Z$ - certainly not a homeomorphism.
Also, you're right that a map of schemes requires a map of the structure sheaves. This is not derivable from the map on the underlying space. Instead it comes from the map of rings. Indeed, the idea is that for a basic open set $D(f) \subseteq \operatorname{Spec} B$, its pullback under the map induced by $\phi: B \longrightarrow A$ is going to be $D(\phi(f))$. Recall that $\Gamma(D(f), \operatorname{Spec} B) = B_f$. We're looking for a map $\Gamma(D(f), \operatorname{Spec} B) \longrightarrow \Gamma(D(\phi(f)), \operatorname{Spec} A)$ to define our map of structure sheaves. We'll then take this to be the map $B_f \longrightarrow A_{\phi(f)}$, which we get from the universal property of localization applied to $\phi$. Now, I've only defined the map on a basis of open sets, but general properties of sheaves imply that this is enough. Also, you need to check that the induced maps on stalks are all local, but I won't show this. Here is a reference to the Stacks project for this fact.
Now I hope this construction explains why a ring homomorphism induces a reverse map of affine schemes, structure sheaf and all. But I did say that you cannot derive this map purely from the map on the underlying topological spaces, and I'd like to explain that further. Indeed, in defining a map of affine schemes it would suffice to just specify a continuous map if, for instance, the forgetful functor $F: \mathbf{AffineSchemes} \longrightarrow \mathbf{Top}$ was fully faithful. In fact, it is neither full mor faithful.
For fullness, consider maps $\operatorname{Spec} \mathbb Z \longrightarrow \operatorname{Spec} \mathbb Z$. There is only one ring homomorphism $\mathbb Z \longrightarrow \mathbb Z$, so the only map between the affine schemes $\operatorname{Spec} \mathbb Z \longrightarrow \operatorname{Spec} \mathbb Z$ is the identity. However, there are many continuous maps between the underlying spaces $F(\operatorname{Spec} \mathbb Z) \longrightarrow F(\operatorname{Spec} \mathbb Z)$. For instance, there are infinitely many constant maps, all of which are continuous. Hence, the forgetful functor $F$ cannot be full. In other words, not every map of the underlying topological space can arise from a map of affine schemes.
Now for faithfulness, consider $\mathbb Q(i)$. We have two automorphisms of this field, the identity and complex conjugation. They therefore define two automorphisms of the affine scheme $\operatorname{Spec} \mathbb Q(i)$. However, $\mathbb Q(i)$ is a field so the topological space of the affine scheme, $F(\operatorname{Spec} \mathbb Q(i))$, is a single point. There is only one continuous map from a point to itself, so $F$ cannot be faithful.  That is, it is impossible in general to take a continuous map between affine schemes and derive a corresponding map on the structure sheaves as the same continuous map can come from many scheme maps.
These examples also show that the forgetful functor $\mathbf{Schemes} \longrightarrow \mathbf{Top}$ is neither full nor faithful, so you cannot define a map of schemes purely from a continuous map. I'd also like to point out that unlike affine schemes, a map between general schemes is not determined by a (reversed direction) ring homomorphism on global sections. Indeed, consider projective space over an algebraically closed field $k$. The global sections of $\mathbb P_k^n$ is $k$ for all $n$. There are many maps between projective spaces that are constant on global sections. For instance, linear automorphisms of $\mathbb P_k^n$ are defined by matrices in $PGL_n(k)$, and in general this group is nontrivial.
