If $U$ is an open subset such that the codimension of $X-U$ is greater or equal to 2. Then by defition of codimension, $\min\{\dim \mathcal{O}_{X,x}:x\in X-U\}\geq 2$, i.e. points of $X-U$ are all of codimension $\geq 2$. Thus the points of codimension $0$ and $1$ lie in $U$.
It's easy to see that a point is of codimension 0 if and only if it is a generic point of $X$. So $U$ contains all generic points, its closure must be $X$, so $U$ is open dense.
Now we can safely pick any non-empty open affine subset to consider the restriction of our desired isomorphism.
Pick an affine open covering $\{U_i\}_{i\in I} $ of $X$, assuming we have isormophisms $\Gamma(U_i,\mathcal{O}_X)\to \Gamma(U_i \cap U,\mathcal{O}_X)$, we now try to deduce the isomorphism $\Gamma(X,\mathcal{O}_X)\to \Gamma(U,\mathcal{O}_X)$ from the sheaf properties, the exact sequence of sheaves.
It's easy to see that the isomorphism follows from the isomorphisms $\Gamma(U_i\cap U_j,\mathcal{O}_X)\to \Gamma(U_i \cap U_j \cap U,\mathcal{O}_X)$ for all $i$ and $j$ with a simple diagram chase. But $U_i \cap U_j$ is not necessarily affine.
Nevertheless it can be shown that $U_i \cap U_j$ can be covered by simultaneously distinguished open subsets of $U_i$ and $U_j$, see this question in StackExchange.
Now we just apply the same argument to $U_i \cap U_j$ with this covering, this time the intersection of two simultaneously distinguished open subsets is open affine, then we are done.
Best Answer
Every affine scheme is quasi-compact. Indeed, a collection of basic open sets $D(f_i)$ cover $\operatorname{Spec} A$ iff there is no prime ideal that contains all the elements $f_i$. This just means that the ideal $I$ generated by the elements $f_i$ is the entire ring $A$. An ideal $I$ is the entire ring iff $1\in I$. But if $1\in I$, then $1$ is a linear combination of the generators $f_i$, and this linear combination involves only finitely many of the generators. So, there are finitely many $f_{i_1},\dots,f_{i_n}$ which generate $A$ as an ideal, which then means that $D(f_{i_1}),\dots,D(f_{i_n})$ cover $\operatorname{Spec} A$ and are a finite subcover of our original cover.