Let R be a ring and give X = \operatorname{Spec} R the Zariski topology, whose basic open sets are D(f) = \{\mathfrak{p} : f \notin \mathfrak{p}\}. The structure sheaf \mathcal{O}_X assigns to D(f) the localization R_f, the ring of fractions a/f^n. If D(f) \subset D(g), some power of f lies in (g), so g becomes invertible in R_f and restriction is the map R_g \to R_f. In particular R_f depends only on the open set D(f): for example D(x) = D(x^2) and R_x = R_{x^2} [1].
The sheaf conditions hold because of an algebraic partition of unity. If D(f_1), \ldots, D(f_r) cover X, no prime ideal contains all the f_i, so they generate the whole ring, and so do f_1^m, \ldots, f_r^m: we can write 1 = h_1 f_1^m + \cdots + h_r f_r^m. Fractions a_i/f_i^m on D(f_i) that agree on the overlaps then glue to h_1 a_1 + \cdots + h_r a_r, an element of R, and in particular \mathcal{O}_X(X) = R.
For R = \mathbb{R}[x] the sets D(x) and D(x - 1) cover the line, and 1 = x - (x - 1) and 1 = x^2(3 - 2x) + (x - 1)^2(2x + 1) are partitions of unity for m = 1 and m = 2. In the figure the term with x^m vanishes to order m at 0, which is exactly what is needed to cancel the denominator x^m of a section on D(x), and the other term does the same at 1.
The stalk of \mathcal{O}_X at a point \mathfrak{p} is the local ring R_{\mathfrak{p}}, so X is a locally ringed space, a space with a sheaf of rings whose stalks are local rings. An element f \in R has a value at \mathfrak{p} in the residue field R_{\mathfrak{p}}/\mathfrak{p}R_{\mathfrak{p}}, and this value is 0 exactly when f \in \mathfrak{p}. The pair (\operatorname{Spec} R, \mathcal{O}_X) is the affine scheme of R.
A scheme is a locally ringed space (X, \mathcal{O}_X) in which every point has an open neighborhood U such that U, with the restriction of \mathcal{O}_X, is isomorphic to an affine scheme [2]. Schemes are built by gluing affine schemes along isomorphic open subsets, in the same way that manifolds are glued from charts.
The projective line \mathbb{P}^1 over a field k is glued from \operatorname{Spec} k[t] and \operatorname{Spec} k[s]. The open set D(t) = \operatorname{Spec} k[t, 1/t] of the first line is identified with the open set D(s) of the second by s = 1/t [1]. Each line misses one point of \mathbb{P}^1: the point s = 0 of the second line is the point at infinity of the first, and the point t = 0 is the point at infinity of the second.
In the figure the two real lines bend into one circle, the real points of \mathbb{P}^1. The tick t = 2 lands on s = 1/2, the ticks t = 1 and s = 1 meet, and the gap at the top of the first line is filled by s = 0. Gluing by s = t instead of s = 1/t also gives a scheme, the line with two origins, but not the projective line.
A global function on \mathbb{P}^1 is a pair f(t) \in k[t] and g(s) \in k[s] with f(t) = g(1/t) in k[t, 1/t]. The left side contains only nonnegative powers of t and the right side only nonpositive ones, so both sides are the same constant, and \mathcal{O}(\mathbb{P}^1) = k. If \mathbb{P}^1 were affine, it would be the spectrum of k, a single point, so \mathbb{P}^1 is not affine.
An open subset of an affine scheme need not be affine either. Let U = D(x) \cup D(y) be the plane \operatorname{Spec} k[x, y] without the origin. A function on U is a/x^m on D(x) and b/y^n on D(y) with a y^n = b x^m, and unique factorization in k[x, y] gives x^m \mid a, so the function is a polynomial: \mathcal{O}(U) = k[x, y], the same ring as for the whole plane [1].
In the figure the graph of every function on U passes over the missing origin without a break. If U were affine, it would be \operatorname{Spec} k[x, y], which contains the origin. Another way to see this: x and y have no common zero on U, yet every ax + by vanishes at the origin, so ax + by = 1 has no solution in \mathcal{O}(U), which cannot happen on an affine scheme.
A scheme can also have nilpotent functions. \operatorname{Spec} k[x]/(x^2) has a single point, like \operatorname{Spec} k, but the function x on it is not 0, although its value at the point is 0. Dividing by the nilpotent elements gives the reduced scheme, which has the same points and keeps only the functions that are determined by their values.