The stock market has been soaring in recent weeks, finishing out the year by reaching new highs. Over the past 12 months, the Dow Jones Industrial Average is up by around 13%, the S&P 500 has surged by more than 24%, and the Nasdaq Composite is up by a whopping 43%.

Many people are excited about this surge, believing that this is the start of a new bull market. But others are still apprehensive, concerned that this is only a temporary rally before stock prices fall again.

If you're not sure whether to invest now or wait, you're not alone. While nobody knows for certain where the stock market is headed, here's what history says about investing in times like these.

How safe is the stock market right now?

It can be daunting to invest when the future is uncertain, but history shows that it doesn't necessarily matter when you buy. What matters most is how long you hold your investments.

Building wealth in the stock market is a long-term strategy. There will always be short-term fluctuations, but the market itself is incredibly consistent over time. The longer you're able to stay invested, the more protected you'll be against volatility.

Analysts at Crestmont Research studied the S&P 500's historic 20-year total returns to determine how many of those periods ended in positive gains. They found that every single 20-year period resulted in positive total returns. This means that if you had invested in an S&P 500 tracking fund at any point in history and simply held it for 20 years, you'd have made money.

In the past two decades alone, the market has earned positive total returns despite experiencing some of the most severe bear markets and recessions in history during that time.

^SPX Chart

^SPX data by YCharts.

Even if you invest at a "bad" time, keeping a long-term outlook can still reduce the impact of volatility. For example, say you had invested in an S&P 500 index fund in February 2009 -- right before the index bottomed out amid the Great Recession. Your investment would have almost immediately lost value, and that may have seemed like a terrible moment to invest at the time.

But by the end of the year, you'd still have earned returns of more than 35%. Within five years, you'd have seen returns of nearly 116%.

^SPX Chart

^SPX data by YCharts.

Nobody can predict the market's short-term performance. But even if you invest at a sub-optimal time right before stock prices drop, you're still likely to see positive long-term returns if you simply stay invested.

The key to building long-term wealth

In addition to keeping a long-term outlook, it's equally important to ensure you're choosing the right investments. Not all stocks will be able to recover from downturns, and if the market does end up taking a turn for the worse, investing in the wrong places could be costly.

There's not necessarily a right or wrong way to invest, but the safest stocks are the ones from healthy companies with solid fundamentals. This includes everything from strong financials to a capable leadership team to a competitive advantage in the industry.

Healthy stocks will still experience short-term volatility, but they're far more likely to recover and see long-term growth. The more of these stocks you have in your portfolio, the safer your money will be -- and the more you can potentially earn over time.

It's unclear exactly where stock prices are headed in the near term. But by investing in the right places and staying invested for as long as possible, you can keep your money safer, no matter what happens with the market.