While it's difficult to use, I think Donald Knuth's TeX is probably the best written piece of software created.
Answer by Victor Eijkhout:
Around 1978,was getting dissatisfied with the quality of computer typesetting. He thought he could solve the problem on his sabbatical. Ok, it took him 8 years, but solve it he did. The typesetting system produces beautiful output for ordinary text, and is unsurpassed when it comes to mathematics.
As a result, it is used by 99 percent of mathematicians, at least half of all computer scientists and physicists. It is even used by people with strange alphabets (apparently there is an Old-Icelandic dictionary done in it) or strange design demands (religious texts with commentary and commentary on the commentary).
Since TeX was written back when computers were smaller and slower, the code is actually pretty tricky to be as efficient as possible. And yet it is basically bug-free. Knuth writes you a cheque if you can find a bug in the code, which happens almost never.