Vakil's Foundations of Algebraic Geometry, Exercise 7.3.F
Solution 1:
I think it is not the morphism $\operatorname{Spec}(A)\rightarrow \operatorname{Spec}\mathbb{Z}$ that should be considered, but rather the inclusion $\iota: U \rightarrow \operatorname{Spec}(A)$, where $U$ is the complement of $Z$. Using the result that a morphism being affine is an affine local property, we hope to find an affine open cover $V_i$ of $\operatorname{Spec}(A)$, where $U$ intersect each affine open piece (i.e. $\iota^{-1}(V_i)$) is also affine, which would then tell us that $U = \iota^{-1}(\operatorname{Spec}(A)$ is also affine.
The hypothesis says that we may cover $\operatorname{Spec}(A)$ by open subschemes $V_i$, such that $Z\cap V_i$ is locally cut out by some element of $\mathcal{O}_X(V_i)$. By covering each of the $V_i$ with affine pieces if necessary and considering the restriction of each element of $\mathcal{O}_X(V_i)$, we may therefore assume that the $V_i$ are affine open subsets of $\operatorname{Spec}(A)$, say $V_i \cong \operatorname{Spec}(A_i)$, and $Z\cap V_i = V(f_i)$ for some $f_i \in A_i$. In particular, the inclusion $U \cap V_i$ is just the inclusion of the distinguished affine open subset $D(f_i)$, and so is affine, as desired.