Stocks can swing wildly from year to year, as investors were reminded in 2022. While investors can't control the markets, some stocks allow them to sleep well at night better than others.

Microsoft (MSFT -2.45%) and Apple (AAPL 0.52%) provide essential services people use every day, and new technologies and product development are only strengthening their long-term growth prospects. Here's why these two top stocks will continue winning for investors over the long term.

1. Microsoft

You would be hard-pressed to find a more solid investment than Microsoft. Windows and Office software are widely used in the consumer and enterprise spaces, and Microsoft has successfully migrated these products to subscription services in recent years, leading to robust growth in earnings that helped fuel the stock's performance.

Microsoft is posting balanced revenue and earnings growth despite a challenging business environment. In the most recent quarter, Office and cloud services growth drove double-digit year-over-year increases in revenue and earnings per share on a constant-currency basis.

Thanks to the company's investment in OpenAI -- the creators behind popular artificial intelligence (AI)-powered text app ChatGPT -- Microsoft is also bringing smart AI tools to its Office suite, which could drive more demand.

Another reason to consider Microsoft a solid investment for the long haul is its booming enterprise cloud services business. Microsoft Azure has soared to second place behind Amazon over the last five years. Microsoft mentioned on the last earnings call that it now has more than 2,500 Azure OpenAI customers using the service's advanced language models, up an impressive 10-fold over the previous quarter.

These developments make clear that advancing technology only strengthens Microsoft's competitive moat. Microsoft has the cash flows to invest in the latest technologies fueling demand for its core software services, and that's the main reason investors can rely on the stock to keep reaching new highs over time, no matter what happens in the near term.

2. Apple

The iPhone maker is another solid stock that should continue to grow in value for years to come, regardless of what happens in the markets in the near term. The stock fell in 2022, along with everything else, but has rebounded around 32% this year. Apple generates enormous amounts of cash from operations, pays a regular dividend, and has an enviable brand.

As noted by last year's dip in the stock price, Apple is not immune from market swings, but investors can count on consistent revenue every year from a loyal customer base that continues to buy more devices. This leads to consistent, robust free cash flow the company can return to shareholders through a growing dividend and share repurchases.

AAPL Chart

AAPL data by YCharts.

Apple said its installed base of devices hit another record in the most recent quarter. It entered the year with more than 2 billion devices across its customer base, up from 1.3 billion at the start of 2018. It's unsurprising that as more customers have purchased an Apple product and spent money on subscriptions and apps, Apple's free cash flow has also grown to $97 billion, or 78% higher than five years ago.

With over 2 billion active devices, you might be wondering how much more Apple can grow. It can keep growing that figure through new product releases and converting customers from competing smartphone manufacturers.

Apple is expected to finally unveil its long-rumored mixed-reality headset next month at the company's annual Worldwide Developers' Conference. While initial sales are expected to be modest at best, especially at the rumored $3,000 price point, the product could grow in importance over the long term like the Apple Watch did after its 2015 launch.

Investors should also expect Apple to find ways to wow customers with new AI features in its existing product lineup, which could convert non-Apple users to try an iPhone. After all, CEO Tim Cook credited the recent increase in the installed base to "a large number of switchers" following the iPhone 14 launch last fall.

Apple's underlying edge over competitors is brand power. And that's why Warren Buffett has sunk billions into the stock in recent years and why investors can expect Apple to keep winning for shareholders for years to come.