Home Depot (NYSE:HD) operates in a highly competitive industry where rivals fight for every sale through pricing, customer service, merchandise selection, and store location. Yet the retailer seems to have a good handle on those varied challenges right now. CEO Craig Menear and his executive team just raised their profit outlook after Home Depot posted a stellar 6% second-quarter sales boost.

Competition sits high on any retailer's list of worries. But Home Depot's biggest risk -- its reliance on a steady housing market -- is completely outside of the company's control.

Here's how management describes the concern its latest annual report:

Our financial performance depends significantly on the stability of the housing, residential construction and home improvement markets.

A uniquely rough period
Last decade's financial crisis provides an unusually clear view into how this risk can affect investors. As home prices sank and the market for residential investment collapsed in 2007 through 2009, Home Depot's earnings, profitability, and return on invested capital all dove by 40% or more.

HD Return on Invested Capital (TTM) data by YCharts.

Management went for three years without raising the company's dividend, and the stock fell by as much as 50% as sales shrank from a high of $79 billion in 2007 to $71 billion in 2009.

Yet the market eventually turned positive again. And when that happened, shareholders witnessed an astounding improvement in Home Depot's financial position.

Annual earnings have more than doubled since 2010. Operating margin is up to 13% from the low of 7%. And return on invested capital has surged to 25% as sales passed $83 billion a year.

Home Depot's dividend is 150% higher; its 2015 raise of 26% even beat last year's stellar 21% payout hike. The stock has more than tripled in five years, compared to a 79% gain in the broader market.

Making your own luck
While positive market forces created a nice tailwind these past five years, Home Depot's management team deserves credit for taking full advantage of the rebound. After all, the retailer has significantly outperformed its national rival Lowe's (NYSE:LOW) on all the key metrics mentioned above. The most dramatic difference can be seen in ROIC, which is a good measure of management's efficiency. Both company's ROIC's took similar hits during the housing crisis, but the rebound has all belonged to Home Depot.

HD Return on Invested Capital (TTM) data by YCharts.

The big-picture performance gap has carried on into the current fiscal year. Home Depot most recently posted a 4% gain in customer traffic for the second quarter, compared to Lowe's 1.2% improvement.

Looking ahead
One of the best single metrics for tracking whether Home Depot has the wind at its back in the housing market is private residential fixed investment, which is a government statistic that shows the annual rate of spending on home improvement across the United States. That figure peaked in 2006 at $900 billion before plummeting to below $400 billion at the bottom of the crisis. The rebound has been marked by a steady improvement to the most recent $600 billion mark, which we haven't seen since 2007.

Source: Federal Reserve Economic Data.

What's most interesting about this chart is that Home Depot managed to set so many record financial and operating results despite the fact that we're far from the last peak in home improvement spending. That implies there are a lot of things that the management team is doing right -- beyond simply riding the ups and downs of the market.

Good leadership doesn't protect shareholders from economic risks like a historic collapse in demand. But it does raise the likelihood that their investment will pay off over the long term, so long as they can patiently wait out the storm.

Demitrios Kalogeropoulos owns shares of Home Depot. The Motley Fool recommends Home Depot. Try any of our Foolish newsletter services free for 30 days. We Fools may not all hold the same opinions, but we all believe that considering a diverse range of insights makes us better investors. The Motley Fool has a disclosure policy.