Investors are told, and rightly so, that long-term investing is an excellent strategy for building wealth in the stock market. But most people may not realize just how ruthless the corporate world is.

According to studies by consulting firm McKinsey & Company, the average life span of a company in the S&P 500 -- yep, that index of 500 of America's most prominent corporations -- is just 18 years. Most companies eventually get bought out, merge with another, or go bust.

Finding companies with staying power that can steadily grow for decades, building generational wealth along the way, isn't easy. That doesn't mean it's impossible.

Here are three winning stocks with the rock-solid foundations to continue thriving for years.

1. Realty Income

Real estate is one of humankind's oldest trades, and since nothing can replace good 'ol land, it will remain an industry in which you can invest money forever. Most real estate stocks will be real estate investment trusts (REITs), structured to pay at least 90% of their taxable profits as dividends. Realty Income (O -0.17%) is one of the best REITs money can buy.

Realty Income focuses on leasing retail properties. Consumers routinely spend money in these places, like grocery and convenience stores, pharmacies, dollar stores, and fast-food restaurants. The idea is that these businesses are recession-proof, so they shouldn't struggle to pay rent. Additionally, Realty Income uses net leases, which put expenses like taxes, insurance, and maintenance on the tenant. As a result, Realty Income enjoys stable revenue streams, making it a more reliable investment.

Paying a steadily rising dividend is engrained in Realty Income's culture. It even advertises itself to shareholders as "The Monthly Dividend Company." Shareholders enjoy a monthly payout, and Realty Income has raised it for 29 consecutive years and counting. With an impressive resume spanning decades and a balance sheet with a rock-solid A3 credit rating from Moody's, it's hard to see the stock going anywhere anytime soon.

2. Nike

Sports have long been ingrained in global culture, and no company is more prominent in the sports industry than Nike (NKE 0.19%). The company burst on the scene in the 1970s, and its iconic swoosh logo has become synonymous with names like Michael Jordan and other legendary athletes.

Nike is most famous for its sneakers. It also sells clothing and manufactures jerseys for North America's three largest professional sports leagues: the National Football League, the National Basketball Association, and Major League Baseball.

There isn't much about a shoe that another company can't copy. However, you can't mimic Nike's two biggest advantages: brand power and deep pockets. Nike is generating more than $6 billion in annual free cash flow, over four times its closest competitor, Adidas. That means competitors can't outbid Nike for the sporting world's brightest stars.

And with a $150 billion market cap today, it's unlikely investors have to worry about Nike being acquired. Nike's brand stands on its own, and even playing hypotheticals, Nike is too big at this point for most companies to afford. The swoosh has worldwide appeal, setting Nike up for long-term growth as emerging markets mature. Those consumers who have watched LeBron James or other famous Nike athletes growing up will gravitate toward the brand when they set out to buy new sneakers.

3. Costco Wholesale

You wouldn't think of a store where you buy groceries and random odd finds as a company with a cult-like following, but Costco Wholesale (COST 1.01%) has done just that. The big-box retailer is arguably more famous for selling a soda and 16-ounce hotdog for $1.50 than anything. To shop there, one must pay for a membership, which is the secret sauce to Costco's profits while selling its merchandise at razor-thin margins.

Costco Wholesale brings in a staggering $245 billion in revenue but generates just $8.8 billion in free cash flow, less than $0.04 per dollar. That makes it hard for most competitors to undercut the company's prices and deals. Maybe a company like Walmart can throw its hat in the ring, but people love shopping at their local Costco. According to research by Comparably, Costco has a net promoter score of 56 versus just eight for Walmart.

Additionally, Costco has an old-school management style. Its leadership has avoided loading up the balance sheet with debt and instead pays for share repurchases and dividends primarily with organic profits. There is $17 billion in cash on the balance sheet against just $7 billion in debt.

Costco is so well known that the company doesn't have to spend on sales and marketing. How cool is that? With such strong brand power and fortress-like financials, Costco is poised to build wealth for investors for a long time.