Buying and holding stocks for the long term has many advantages. For one, it eliminates trying to time the market with buys and sells. Secondly, it allows compounding to occur over many years, increasing returns for investors. The trick is to know which companies to buy and hold in the first place.

As we head into 2024, here are five stocks worth buying and holding for the long term. Each of these companies has advantages that make them great choices to hold for decades. Let's dig in and see why.

1. Realty Income

Exposure to the real estate market makes sense in any well-diversified portfolio. Fortunately, this can be done without owning any actual real estate. Realty Income (O -0.17%) is a real estate investment trust (REIT), which means it has to pay out at least 90% of its taxable income as a dividend to its shareholders. The REIT takes this very seriously and even refers to itself as "The Monthly Dividend Company." Currently, its dividend is yielding 4.9%.

Realty Income rents out properties to clients in 85 industries. The company focuses on renting in sectors that are resilient to economic downturns, making the rents they collect more secure should we enter into a recession at any point. This strategy has proved successful as Realty Income has produced a 13% compound annual return since its debut as a public company almost 30 years ago.

2. Etsy

With the holidays in the rearview mirror, there's a chance someone you know may have visited Etsy's (ETSY 0.34%) website for a handmade gift for a loved one. Etsy's two-sided marketplace connects sellers of unique handmade items with buyers looking for gifts they can't get anywhere else. Etsy currently has 97 million active buyers and 8 million active sellers on its platform.

In the recently reported third quarter of 2023, Etsy reported year-over-year active seller growth of 19% and an increase in active buyers of 3%. The company would like both of these metrics to accelerate, but these Q3 results did represent the third straight quarter of improved growth, suggesting it may be seeing interest in its marketplace is starting to pick up steam.

Trading for only 4 times sales, Etsy is almost as cheap as it's ever been, providing a margin of safety for investors as the company works to reaccelerate growth.

3. Amazon

Amazon (AMZN 3.43%) has been a massive winner for investors over its lifetime, but the last few years have been nothing to write home about. Over the past three years, Amazon stock is down 3%, vastly underperforming the S&P 500's return of 28%. Much of this underperformance is a result of the pandemic, when Amazon doubled its distribution footprint to keep up with demand but then needed to right-size once spending on its platform normalized.

The good news is that Amazon seems to be back on the right track. Operating income in the third quarter totaled $11 billion, up 343% from a year ago. This turnaround is being driven by the e-commerce business, which had been operating income negative during 2022 but has steadily turned that around over the year.

4. Apple

With its seemingly ubiquitous devices, it's no surprise that Apple (AAPL -0.35%) would be on the short list of stocks to hold for the next 20 years. However, it's not the devices that could drive Apple's returns for the next two decades. Apple now has more than 2 billion active devices worldwide, which is driving one of the company's fastest-growing segments: its services.

Apple's services segment comprises subscription products like Apple Music, iCloud, and Apple TV+. This segment saw revenue growth of 10% in the company's fiscal 2024 year (ended Sept. 30), and services revenue now represents 22% of total revenue, up from 20% in fiscal 2023. This revenue is also higher-margin, which has helped Apple increase its gross margin by 80 basis points in fiscal 2024 even as revenue decreased by 3%.

5. Shopify

E-commerce provider Shopify (SHOP 1.11%) powers the websites and behind-the-scenes mechanics of thousands of companies. By helping businesses of all sizes manage their logistics, Shopify has become an integral part of the e-commerce landscape.

After experiencing inflated revenue growth during the pandemic, Shopify stock has struggled as its business has normalized. However, the most recently reported quarter, Q3 of 2023, suggests that things are heading in the right direction on both the top and bottom lines. Revenue increased by 25% year over year, and gross margin improved by 410 basis points. This helped operating income and net income turn positive after both were a loss in Q3 of 2022.