"It's far better to buy a wonderful company at a fair price than a fair company at a wonderful price."

- Warren Buffett

Everyone wants to buy stocks that are cheap. Everyone wants a bargain.

But when it comes to investing, this desire puts the cart before the horse.

What matters first and foremost, at least according to Warren Buffett, arguably the greatest investor to ever live, is the quality of the company of you're investing in.

"It's far better to buy a wonderful company at a fair price than a fair company at a wonderful price," Buffett wrote in his 1989 letter to shareholders of Berkshire Hathaway (BRK.A 1.18%) (BRK.B 1.30%).

If you're an investor, it'd behoove you to think long and hard about this simple admonition.

Simplicity is the ultimate sophistication
Like many of Buffett's best-known quotes, the substance and story behind his phrase about preferring wonderful companies at fair prices gets obscured by the simplicity of its delivery.

"Simplicity is the ultimate sophistication," wrote Leonardo da Vinci.

What's captured in Buffett's short and pithy maxim is the accumulation of knowledge that he acquired throughout his first 25 years at the helm of Berkshire Hathaway.

More specifically, it represents the lesson he learned about adhering too closely for too long to the value investing philosophy pioneered by his friend and mentor Benjamin Graham.

In a section of his 1989 letter to shareholders titled "Mistakes of the First Twenty-five Years (A Condensed Version), Buffett conceded that his "first mistake, of course, was in buying control of Berkshire."

Though I knew its business -- textile manufacturing -- to be unpromising, I was enticed to buy because the price looked cheap. Stock purchases of that kind had proved reasonably rewarding in my early years, though by the time Berkshire came along in 1965 I was becoming aware that the strategy was not ideal.

If you buy a stock at a sufficiently low price, there will usually be some hiccup in the fortunes of the business that gives you a chance to unload at a decent profit, even though the long-term performance of the business may be terrible. I call this the "cigar butt" approach to investing. A cigar butt found on the street that has only one puff left in it may not offer much of a smoke, but the "bargain purchase" will make that puff all profit.

Unless you are a liquidator, that kind of approach to buying businesses is foolish. First, the original "bargain" price probably will not turn out to be such a steal after all. In a difficult business, no sooner is one problem solved than another surfaces— never is there just one cockroach in the kitchen. Second, any initial advantage you secure will be quickly eroded by the low return that the business earns. For example, if you buy a business for $8 million that can be sold or liquidated for $10 million and promptly take either course, you can realize a high return. But the investment will disappoint if the business is sold for $10 million in ten years and in the interim has annually earned and distributed only a few percent on cost. Time is the friend of the wonderful business, the enemy of the mediocre.

I quoted this passage in its entirety not because I'm unwilling to paraphrase, but rather because it contains critical insights for investors.

Indeed, not only does it represent a clear repudiation of Buffett's former philosophy, but it also reveals his willingness to both acknowledge and learn from former mistakes.

"There's no rule that says you have to learn by failing yourself," my colleague Morgan Housel recently wrote. "It is far better to learn vicariously from other people's mistakes than suffer through them on your own."

And there are few better people to learn from -- mistakes and all -- than the Oracle of Omaha.

Buying wonderful companies at fair prices
While volumes could be written about Buffett's evolved approach to investing, captured in his quote about buying wonderful companies at fair prices, the basic analysis is very simple.

What matters most is the quality of the company itself. I'd encourage you, for instance, to contemplate the adjective Buffett used: "wonderful."

In its purest form, that's a powerful word. Wonderful meals are rare. As are wonderful books and movies. And the same can be said of companies.

This is exemplified by Berkshire's portfolio of common stocks. Despite having untold billions of dollars to invest, Buffett has allocated the funds to a select few businesses, many of which are the absolute best in the world at what they do.

He accumulated a massive stake in Wells Fargo (WFC 2.74%) long before it conquered the financial crisis by devouring Wachovia and thereby more than doubling in size. He saw it early on in American Express (AXP 6.22%) when its business revolved principally around traveler's checks. And he recognized it, albeit belatedly by his own admission, in Coca-Cola (KO 2.14%), which oversees the most recognizable brand on earth.

The point here is that great investors invest in great companies.

Now, just to be clear, this isn't to say that stock price and valuation don't matter. Indeed, nothing could be further from the truth.

"For the investor, a too-high purchase price for the stock of an excellent company can undo the effects of a subsequent decade of favorable business developments," Buffett wrote in 1982.

But the key to remember is that this is the second and subsidiary step in the analysis.

Consequently, the next time your neighbor, brother-in-law, or coworker talks about how such-and-such stock is a cheap right now, your response should be: "That may be so, but is it a wonderful company?"