Prime Spectrum of a ring: why is Geometry captured by local rings? If a function $f: \mathbb R \rightarrow \mathbb R$ is continuous, $f$ is locally invertible at all non-vanishing points. That is, for all points $x_0$ such that $f(x_0) \neq 0$, there exists an open neighbourhood $U$ of $x_0$ and a function $g: U \rightarrow \mathbb R$, such that for all $u \in U$, $(f \times g)(u) = f(u) \times g(u) = 1$.
Is the converse true? Is a function which is locally invertible at all non-vanishing points continuous?
It seems not. Consider the function $
f(x) = \begin{cases}
1 & x \in \mathbb Q \\
2 & \text{otherwise}
\end{cases}
$
This has an inverse function
$
g(x) = \begin{cases}
1 & x \in \mathbb Q \\
1/2 & \text{otherwise}
\end{cases}
$
even though $f$ is discontinuous everywhere. So clearly, this locally invertible definition is very far away from giving us continuous functions.
I am now unmotivated about the spectrum of a ring. It is this "local inversion"
property that (I thought) motivates the definition of the structure sheaf
on the spectrum of a ring. In $\operatorname{Spec}(A)$, the ring of functions around a  point(prime)
$\mathfrak p$ is $A_\mathfrak p$. So all functions (ring elements) which are not zero
at $\mathfrak p$ will be forced to become invertible by way of localization.
But this definition does not seem strong enough to actually capture what
we want --- it allows lots of pathological rings of functions, from which
we cannot recover the structure of the original space. The proof I know which recovers the original space given the ring of functions needs continuous
functions to apply Urhyson's lemma [this can be found in Atiyah Macdonald, Chapter 1, Exercise 26)
What am I missing?
 A: It is a fact that a (real or complex valued) continuous, or continuously differentiable, or smooth, or analytic, etc. function that vanishes nowhere has a multiplicative inverse in the same category. Moreover, by continuity, a function can only vanish on a closed set. Therefore the sheaf of such functions on a topological space has the property that its stalks are local rings. For irreducible algebraic varieties defined in the classical way we have rational functions, the sheaf of regular functions has the same property. For not necessarily irreducible algebraic varieties we can't really talk about rational functions but a closer analysis of the sheaf of regular functions on irreducible affine algebraic varieties reveals that it is not necessary to go via rational functions in the first place, and that is how we get to the definition of the structure sheaf of a general affine scheme. The fact that the stalks are local rings is, in some sense, incidental.
Let $k$ be an algebraically closed field and let $X$ be a subset of $k^n$. For the purposes of this answer, a regular function on $X$ is a function $f : X \to k$ for which there exist polynomials $p$ and $q$ over $k$ such that $q (x) \ne 0$ for all $x \in X$ and $f (x) = p (x) / q (x)$ for all $x \in X$. Let $\mathscr{O} (X)$ be the set of regular functions on $X$. Then:

If $X$ is an irreducible closed subset of $k^n$, then the assignment $U \mapsto \mathscr{O} (U)$, where $U$ varies over the open subsets of $X$, defines a subsheaf $\mathscr{O}_X$ of the sheaf of $k$-valued functions on $X$.

There is actually a claim to be checked here, namely that regularity of functions is a local property, but I leave that to you. The above definition required $X$ to be embedded in $k^n$, but this is actually unnecessary. Firstly:

If $X$ is a closed subset of $k^n$ and $f : X \to k$ is a regular function, then there is a polynomial $p$ over $k$ such that $f (x) = p (x)$ for all $x \in X$.

More generally:

Let $X$ be a closed subset of $k^n$, let $q$ be a polynomial over $k$, and let $U = \{ x \in X : q (x) \ne 0 \}$. If $f : U \to k$ is a regular function, then there exist a positive integer $m$ and a polynomial $p$ over $k$ such that $f (x) = p (x) / q (x)^m$ for all $x \in X$.
Moreover, if $U$ is dense in $X$, then the unique homomorphism $k [x_1, \ldots, x_n, u] \to \mathscr{O} (U)$ sending $x_1, \ldots, x_n$ to the respective coordinate functions $U \to k$ and $u$ to the regular function on $U$ defined by $1 / q$ has kernel $(I (X) + (q u - 1))$, where $I (X)$ is the ideal of polynomials vanishing on $X$.

Indeed, since $f : U \to k$ is a regular function, there exist polynomials $p_1$ and $q_1$ such that $q_1 (x) \ne 0$ for all $x \in U$ and $f (x) = p_1 (x) / q_1 (x)$ for all $x \in U$. By the Nullstellensatz, $\sqrt{I (X) + (q_1)} \supseteq \sqrt{I (X) + (q)}$; in particular, there exist a positive integer $m$ and $r \in k [x_1, \ldots, x_n]$ and $s \in I (X)$ such that $q_1 r + s = q^m$. Hence,
$$\frac{p_1 (x)}{q_1 (x)} = \frac{p_1 (x) r (x)}{q_1 (x) r (x)} = \frac{p_1 (x) r (x)}{q (x)^m}$$
for all $x \in U$, so we may take $p = p_1 r$.
Given a general element of $k [x_1, \ldots, x_n, u]$, say $p_0 + p_1 u + \cdots + p_m u^m$, where $p_0, \ldots, p_m$ are polynomials in $x_1, \ldots, x_n$ over $k$, we have
$$p_0 (x) + \frac{p_1 (x)}{q (x)} + \cdots + \frac{p_m (x)}{q (x)^m} = 0$$
for all $x \in U$ if and only if
$$p_0 (x) q (x)^m + p_1 (x) q (x)^{m - 1} + \cdots + p_m (x) = 0$$
for all $x \in U$. Since $U$ is dense in $X$, the second equation actually holds for all $x \in X$, so
$$p_0 q^m + p_1 q^{m - 1} + \cdots + p_m \in I (X)$$
and hence,
$$p_0 + p_1 u + \cdots + p_m u^m \in I (X) + (q u - 1)$$
as required. ■
The upshot of all this is that, if $X$ is an irreducible closed subset of $k^n$, then the sheaf $\mathscr{O}_X$ can be reconstructed from the ring $\mathscr{O} (X)$ together with the bijection between the maximal ideals of $\mathscr{O} (X)$ and the points of $X$: the above shows that, for a principal open subset $U \subseteq X$, i.e. $U = \{ x \in X : f (x) \ne 0 \}$ for some $f \in \mathscr{O} (X)$, the ring $\mathscr{O} (U)$ is the localisation of $\mathscr{O} (X)$ with respect to the multiplicative set $\{ 1, f, f^2, \ldots \}$. It is easy to check that the restriction maps are the obvious ones. Since the principal open subsets of $X$ form a basis for the topology of $X$, this determines the sheaf $\mathscr{O}_X$. Modulo the introduction of non-maximal prime ideals, this is exactly how one constructs the structure sheaf for a general affine scheme.
