Buying and holding shares of great companies for years -- and yes, even a decade -- is a great way to build wealth

But it's not just about holding on to stocks for a long time; you first need to buy shares of companies with significant leads in their respective markets and businesses that can stand the test of time. Two such companies are Microsoft (MSFT 1.82%) and Amazon (AMZN 3.43%). Here's why they could grow your portfolio over the next decade. 

A person using a computer.

Image source: Getty Images.

1. Microsoft 

Investors have turned their attention to Microsoft lately as the company recently emerged as an artificial intelligence (AI) leader. And the moves the company is making in the AI space right now should help set it up for more growth in the coming years. 

Microsoft was an early backer of OpenAI, the maker of the popular ChatGPT chatbot, and invested $10 billion in the company so far. Microsoft has integrated ChatGPT into its Bing search engine, Microsoft 365 software, and Azure cloud computing services. 

These moves propelled Microsoft to the front of the AI software race, which is shaping up to be a massive market that could be worth an estimated $14 trillion by 2030.

AI services will depend heavily on the cloud, and that should help boost Microsoft's cloud business as its Azure cloud service is currently the second largest. 

Microsoft was already in a great position before AI came along, as companies increasingly relied on Azure to host their products and services. But now that the company is also going all-in on AI, there's even more long-term opportunity. 

Microsoft will surely have AI rivals, but with its partnership with OpenAI and the company swiftly integrating the technology into its services, it is well-positioned for the coming AI race. 

2. Amazon 

Like Microsoft, Amazon has a huge opportunity to benefit from expanding AI software services because the company is a leading cloud computing provider with about 32% of the market. There's more than enough room for both Amazon and Microsoft to benefit from the increasing demand for cloud computing, so I wouldn't worry too much about these two companies competing in the same space. 

Amazon also has opportunities beyond cloud computing, including its e-commerce and advertising.

The company has a dominant position in the e-commerce market, with a 38% share of the U.S. market. E-commerce sales in the U.S. accounted for just 15% of all retail sales in the most recent quarter, showing plenty of room for growth.

Lastly, Amazon has an expanding digital advertising business that generated $9.5 billion in sales in the first quarter. It will have an estimated 13% of the digital ad market by 2026, up from 10% this year. And the company's ad revenue should continue to grow as the U.S. digital ad market rises to an estimated $315 billion by 2025.

Be patient when the hard times come

Holding on to stocks for a decade will test any investors' patience. But patience lets the market swings pass, allowing the potential of leaders such as Microsoft and Amazon to continue developing.  

It's fine to evaluate your investments from time to time. Just make sure that you don't make quick decisions based on temporary market downturns. These two stocks have the potential to boost your portfolio over the long haul. All you need to do is give them time.