What do epimorphisms of (commutative) rings look like? (Background: In any category, an epimorphism is a morphism $f:X\to Y$ which is "surjective" in the following sense: for any two morphisms $g,h:Y\to Z$, if $g\circ f=h\circ f$, then $g=h$. Roughly, "any two functions on $Y$ that agree on the image of $X$ must agree." Even in categories where you have underlying sets, epimorphisms are not the same as surjections; for example, in the category of Hausdorff topological spaces, $f$ is an epimorphism if its image is dense.)
What do epimorphisms of (say commutative) rings look like? It's easy to verify that for any ideal $I$ in a ring $A$, the quotient map $A\to A/I$ is an epimorphism. It's also not hard to see that if $S\subset A$ is a multiplicative subset, then the localization $A\to S^{-1}A$ is an epimorphism. Here's a proof to whet your appetite.

If $g,h:S^{-1}A\to B$ are two homomorphisms that agree on $A$, then for any element $s^{-1}a\in S^{-1}A$, we have
  $$g(s^{-1}a)=g(s)^{-1}g(a)=h(s)^{-1}h(a)=h(s^{-1}a)$$

Also, if $A\to B_i$ is a finite collection of epimorphisms, where the $B_i$ have disjoint support as $A$-modules, then $A\to\prod B_i$ is an epimorphism.
Is every epimorphism of rings some product of combinations of quotients and localizations? To put it another way, suppose $f: A\to B$ is an epimorphism of rings with no kernel which sends non-units to non-units and such that $B$ has no idempotents. Must $f$ be an isomorphism?
 A: No, not every epimorphism of rings is a composition of localizations and surjections.
An epimorphism of commutative rings is the same thing as a monomorphism of affine schemes. Monomorphisms are not only embeddings, e.g., any localization is an epimorphism and the corresponding morphism of schemes is not a locally closed embedding.
Example: Let $C$ be the nodal affine cubic and let $X$ be its normalization. Pick any point $x$ above the node. Then $X\setminus\{x\}\to C$ is a monomorphism (see Proposition below). The corresponding homomorphism of rings is injective but not a localization.
Proposition (EGA IV 17.2.6): Let $f\colon X\to Y$ be a morphism locally of finite type between schemes. TFAE:

*

*$f$ is a monomorphism.

*Every fiber of $f$ is either an isomorphism or empty.

Incorrect remark from 2009: A flat epimorphism $A\to B$ is a localization if $A$ is normal and $\mathbb{Q}$-factorial. This is a result by D. Lazard and P. Samuel. [cf. Lazard, Autour de la platitude, IV, Prop 4.5]
Correction of this remark (May 2022):

*

*A flat epimorphism $A\to B$ is a localization if $A$ is a normal noetherian domain with torsion class group. The result cited above proves this when $A$ is a Dedekind domain.

*When $A$ is a Dedekind domain whose class group is not torsion, then there exists a flat epimorphism $A\to B$ of finite presentation (so an open immersion on Spec) which is not a localization [Lazard, Autour de la platitude, IV, Prop 4.6]. More generally, one can let $A$ be any normal domain whose Cartier class group is not torsion and let $\operatorname{Spec} B$ be the complement of a Cartier divisor $D$ whose class is not torsion.

*The results 1-2 are best understood as follows. When $A$ is normal and locally $\mathbb{Q}$-factorial, then monomorphisms $A\to B$ correspond to subsets $U:=\operatorname{Spec} B\subseteq \operatorname{Spec} A$ such that $U$ is the complement of a, possibly infinite, union of irreducible divisors $\{D_i\}_{i\in I}$ [Raynaud, Un critère d’effectivité de descente, Cor. 2.7]. The complement of a Cartier divisor is always affine so it follows that $U$ is the intersection of the affine open subschemes $U_J$ where $J\subseteq I$ is finite and $U_J:=\operatorname{Spec} A\setminus \bigcup_{i\in J} D_i$. Equivalently, $B$ is the colimit (union if domain) of rings $B_J$ such that $A\to B_J$ is a monomorphism of finite type (an open immersion on Spec). When in addition $A$ has torsion class group, the $B_J$ are localizations (in one element) and it follows that $B$ is a localization.

Remark: There was a seminar on epimorphisms of rings directed by P. Samuel in 1967-68. Raynaud's paper is part of this as well as articles by Lazard that later went into his thesis Autour de la platitude.
A: George Bergman gave me a reference (Isbell's "Epimorphisms and dominions, IV") and a very pretty counterexample. In particular, he says that the characterization of epimorphisms Andrew gave us works for non-commutative rings as well:

Recall that an inclusion  A  in  B  is
  an epimorphism if and only if the
  "dominion" of  A  in  B  is all of  B,
  where this dominion is defined as the
  subring of elements  b  of  B  which
  behave the same under all pairs of
  homomorphisms on  B  that agree on
  elements of A.
Now the Silver-Mazet-Isbell Zigzag
  Lemma for rings says that the dominion
  of  A  in  B  consists of those
  elements of  B  which can be written 
  XYZ,  where  X  is a row,  Y  a
  matrix, and  Z  a column over B,  such
  that  XY  and  YZ  have entries in  A.
  (It is easy to verify that such a
  product is in the dominion of  A -- a
  generalization of the proof that if  Y
  is in  A  and has an inverse in  B, 
  then this inverse is in the dominion
  of  A.)

Let k be a field. Consider the inclusion of k[x, xy, xy2 - y] into
k[x,y]. I claim that this is an epimorphism. Note that it is an
inclusion, no non-units become units, and k[x,y] has no idempotents.
Suppose f and g are two morphisms from k[x,y] to some other
commutative ring which agree on the given subring. Using f(xy)=g(xy)
and f(x)=g(x), we see that f(xy2)=g(xy2):

f(yxy) = f(yx)f(y) = g(yx)f(y) = g(y)g(x)f(y) = g(y)f(x)f(y) = g(y)f(xy) = g(y)g(xy) = g(yxy)

Since f and g agree on xy2-y, they agree on y, so they agree on all of k[x,y].
Finally, to see that the inclusion is not an isomorphism, consider the surjective morphism k[x,y] to k[x,x-1] sending y to x-1. This sends the subring to k[x], which is clearly smaller, so the inclusion of k[x,xy,xy2-y] into k[x,y] must be strict.
A: Here is another perspective on your question. As $\mathbb{Z}$ is the initial object of unital (commutative) rings, one might first of all ask: What do epimorphisms from $\mathbb{Z}$ look like? 
So if $A = \mathbb{Z}$ in the original question, what can $B$ be? The answer to this is known. In fact, these rings $B$ and their classification seem to have been (re)invented several times, as "solid rings" by Bousfield and Kan (see MO question 95160: Solid Rings and Tor), as "T-rings" by R. A. Bowshell and P. Schultz
(Unital rings whose additive endomorphisms commute, Math. Ann. 228 (1977), 197-214, http://eudml.org/doc/162991;jsessionid=07C5F5F5BBD354C0914511776DA20F5E), and the generalisation to Dedekind domains has been done in
W. Dicks and W. Stephenson: Epimorphs and Dominions of Dedekind Domains, J. London Math. Soc. (1984) s2-29(2): 224-228, http://jlms.oxfordjournals.org/content/s2-29/2/224.extract
. (Also, by Martin Brandenburg and myself this summer, before we found these papers ...)
So here is a positive answer under a restrictive assumption: If $A \rightarrow B$ is an epimorphism and $A$ is a Dedekind domain, then $B$ will be built up from localisations and quotients of $A$ by suitable finite products and direct limits. To make "suitable" more specific, here follows a more concrete description (the literature above mostly says "take colimits/pullbacks"; see Martin's comment for other descriptions). I restrict to $A = \mathbb{Z}$ for (mostly notational) simplicity:  
Let $P$ be the set of prime numbers and let $n: P \rightarrow \mathbb{N} \cup \lbrace 0, \infty \rbrace $ be any map (a "supernatural number"). Let $P_{fin}(n)$ be the set of primes $p$ with $n(p) < \infty$. Define
$B_n := \lbrace ((b_p)_p, b_l) \in \prod_{p \in P_{fin}(n)} \mathbb{Z} / p^{n(p)} \times \mathbb{Z}[P_{fin}(n)^{-1}] :$
$$b_p \equiv b_l \text{ mod } p^{n(p)} \text{ for all but finitely many } p \in P_{fin}(n)(b_l) \rbrace$$
(index "$l$" for "localisation part") where:
-- $\mathbb{Z}[P_{fin}(n)^{-1}]$ is the localisation of $\mathbb{Z}$ at the multiplicative set generated by $P_{fin}(n)$, i.e. the subring of $\mathbb{Q}$ generated by $\lbrace p^{-1}: p \in P_{fin}(n) \rbrace$;
-- with $v_p$ being the $p$-adic valuation on $\mathbb{Q}$, $P_{fin}(n)(b_l) := \lbrace p \in P_{fin}: v_p(b_l) \ge 0 \rbrace$ and the condition $b_p \equiv b_l \text{ mod } p^{n(p)}$ makes sense and is to be understood in the subring of $\mathbb{Q}$ where only the $p$'s with $v_p(b_l) < 0$ are inverted.  
Then $B_n$ is in fact a subring of the direct product, and for $n$ ranging over the supernatural numbers, these are all $B$ with injective epimorphisms $\mathbb{Z} \rightarrow B$. (The non-injective ones are just the quotients. With more complicated notation, one could include this case by counting 0 as a prime.)
Here are two easy-to-see properties:


*

*$B_n$ is noetherian if and only if $|P_{fin}(n) \setminus P_0(n) | < \infty$ (where $P_0(n) :=$ set of primes $p$ with $n(p) = 0$), if and only if $B_n$ is the direct product of a quotient and a localisation, namely, $\mathbb{Z}/n \times \mathbb{Z}[P_{fin}(n)^{-1}]$ where by abuse of notation $n$ is the natural number $\prod_{p \in P_{fin}(n)} p^{n(p)}$.

*The non-zero primes of $B_n$ correspond to the ones in $P \setminus P_0(n)$. In particular, $B_n$ is artinian if and only if its Krull dimension is 0 if and only if $|P \setminus P_0(n)| < \infty$. Otherwise, its Krull dimension is 1.
All this remains true cum grano salis for any Dedekind domain $A$ instead of $\mathbb{Z}$. In particular, as soon as $A$ has infinitely many primes, there are epimorphisms $A \rightarrow B$ where $B$ is non-noetherian. On the other hand, if $A$ has only finitely many primes (which by the way makes it a PID), $B$ will be of the form $A/a \times S^{-1}A$ with $a \in A$ and $S \subseteq A$ multiplicative containing all primes dividing $a$ (and possibly 0). In any case, $B$ will be a colimit of products of localisations and quotients as above, so the answer to the question

suppose $f:A \rightarrow B$ is an epimorphism of rings with no kernel which sends non-units to non-units
  and such that $B$ has no idempotents. Must f be an isomorphism?

seems to be yes if $A$ is a Dedekind domain: E.g. in the above setting, non-units to non-units implies $P_0(n) = \emptyset$ and $B$ having no idempotents implies $P_{fin}(n) \setminus P_0(n) = \emptyset$.

Further remarks:
Remark 1 (cf. David Rydh's first remark): Flat epimorphisms (from any unital ring) are localisations for a certain Gabriel topology and have a kind of a calculus of fractions. For a precise statement, see Quelques observations sur les épimorphismes plats (à gauche) d'anneaux by N. Popescu and T. Spircu, Journ. Alg. vol. 16, no. 1, pp. 40-59, 1970, http://dx.doi.org/10.1016/0021-8693(70)90039-6, or Bo Stenström's book Rings of Quotients, theorem 2.1 in chapter XI. 
Remark 2: Further information might be in the papers of H. H. Storrer, e.g.  http://retro.seals.ch/digbib/view?rid=comahe-002:1973:48::11 
Remark 3: I have not checked all the details in the generalisation to Dedekind domains, so beware (at least, Martin and I had reached the same result for PIDs). Also, I do not know if there is a generalisation beyond Dedekind domains; I guess Krull domains might be attackable, but I have not seriously tried.
A: A little searching turned up:
Ring epimorphisms and C(X) by Michael Barr, W.D. Burgess and R. Raphael (article).
They consider this question for rings of the form of continuous functions on a topological space.  They quote the following characterisation of epimorphisms in the category of commutative rings:
Proposition: A homomorphism f : A → B is an epimorphism if and only if for all  b ∈ B there exist matrices C, D, E of sizes 1 × n, n × n, and n × 1 respectively, where (i) C and E have entries in B, (ii) D has entries in f(A), (iii) the entries of CD and of DE are elements of f(A) and (iv) b = CDE.  (Such a triple is called a zig-zag for b.)
This seems a little more complicated than localisation, though I haven't checked the details.
They then go on to prove that
2.12: A subspace Y of a perfectly normal first countable space X induces an epimorphism if and only if it is locally closed.
If I understand all the terminology correctly, then this implies that
C([0,1],ℝ) → C((0,1),ℝ)
is an epimorphism.
There are plenty more references in that article, and it would be nice to have an actual zig-zag for this situation.  But in the spirit of open-source mathematics, I thought I'd post this and see if someone (possibly me later on) can fill in the details.
Added Later: The example I gave: C([0,1],ℝ) → C((0,1),ℝ) is a localisation.  It is obtained by inverting all functions in C([0,1],ℝ) which are zero only at the end-points.  Given a function f ∈ C((0,1),ℝ), there will be a function g ∈ C([0,1],ℝ) which is non-zero apart from at 0 and 1 and which goes to 0 at 0 and 1 faster enough that the product g f also goes to 0 at the end-points.  Then g f is (the restriction of something in) C([0,1],ℝ) and g becomes invertible in C((0,1),ℝ).  So f = g-1 (g f) is in the specified localisation of C([0,1],ℝ).
Indeed, the Barr et. al. paper comments on the fact that in all the examples they consider (function rings), the zig-zag has length 1.  I conjecture that if the zig-zags always have length 1 (for a particular function f: A → B), then B is formed by a localisation on A.  A possibly stronger version of this conjecture would be that this is an if-and-only-if.  In which case, finding a counter-example to Anton's conjecture would involve finding a case where there was a zig-zag of length 2.  I suspect that a universal construction would be the best approach to finding one.
In the spirit of wiki-ness and only doing a little at a time, I'll leave this here.
Added Even Later: (Should I timestamp these?  I know that the system does so, but is it useful to embed them in the edit?)
Here's one direction for my conjecture above.
If B = S-1A, then for b ∈ B, we have b = s-1a for some s ∈ S and a ∈ A.  Then we put C = s-1, D = s, E = b = s-1 a.  Then CD = 1, DE = a, D ∈ f(A), and CDE = b.  So in a localisation, zig-zags have length 1.
A: A special case where epimorphisms are surjective is the category of finite-dimensional commutative $k$-algebras where $k$ is a field. See for example this page in the Stacks Project. 
This may come in handy on occasion; I was trying to convince myself this morning that monomorphisms between cocommutative $k$-coalgebras are those whose underlying functions are injective, and needed the result above as a lemma (first check the result on finite-dimensional cocommutative $k$-coalgebras by taking linear duals on the result above, and then use the fact that every coalgebra is the directed colimit of the system of finite-dimensional subcoalgebras and inclusions between them). 
