Why are there exactly four nucleobases in DNA?
A now deleted answer put this down to an argument based on amino-acid coding in codons but, as pointed out by Konrad Rudolph in a comment, arguments based on codons cannot be correct because the 4-base system likely pre-dates the evolution of protein translation.

So why four?

I suggest that it comes from a combination of factors which make it a 'just right' fit for RNA-based replicators: more would be bad, and less would also be bad. The four bases are able to copy with high reliability because two features combine to exclude mis-pairings: there are two size classes - meaning any purine-purine or pyrimidine-pyrimidine pairing results in incorrect separation between the strands, excluding A-G and C-U bonds; and there are two classes based on number of OH-bonds - which limits the potential for A-C and G-U bonds. More bases must, inevitably, weaken the strength of these exclusionary approaches and increase the number of mispairings. RNA world replicators will have lacked the sophisticated repair and error checking mechanisms of modern lifeforms so this increase in mispairings would likely not be corrected.

Of course, a two base system would be even more capable of reducing errors. I suggest that the advantage of the four base system is that it allows for much more complex 3-dimensional structure to form in RNA and thus enabled a broader range of catalytic capabilities. I've only considered even-numbers because only even numbered systems could use the Watson-Crick style of base pairing.

I'd also suggesting reading Eörs Szathmáry's 2003 paper on this very question.

Addendum

A group in California has just published a paper in which they successfully added a third base pair stably into DNA. I think this could shed some light on why four - you can read about it here or get the full paper here (Nature, so pay walled).