Here are a few things that you could try. But note that they are all capable of failing. They will just reduce the chance of you personally having a lost decade. First a quibble: John Bogle advocates a total stock market index (something like Vanguard's VTSMX) instead of an S&P fund, as the latter represents "only" 85% or so of the US market's total capitalization. Smaller companies behave slightly differently than members of the S&P, so this might provide a small help. Bogle also advocates holding some bonds in addition to equities. I'll expand on that below.
Account for dividends. Just because the value of the index is the same as its value 10 or 20 years ago doesn't necessarily mean that decade was lost. The companies in the S&P are currently paying out an annualized dividend of about 2%. Even if the actual value of the index doesn't change, you're still getting that 2% per year.
Include bonds. As I mentioned above, Bogle recommends holding some bonds. I have seen two common rules. One is to never have less than 20% of your total holdings in bonds, and never have more than 80% of your total holdings in bonds. The other popular rule is to hold your age in bonds. For example, I'm about 30, so I should keep about 30% of my holdings in bonds. Regardless of the split, rebalance periodically to keep yourself at that split. What effect would holding bonds have on a lost decade? To make the math easy, let's say you split your holdings evenly between an S&P fund and 10 year Treasuries. Coincidentally, 10 year T-notes have the same 2% yield as the S&P dividends. If you're getting that on half your holdings, and nothing on the other half, you're netting 1% per year. Not great, but not totally lost. To illustrate the effect of rebalancing, use my example of a 70/30 stock/bond split. The S&P lost about 50% of its value from its peak to the bottom of the market in early 2008. If you only held stock, you would need the market to increase in value by 100% in order for you to recover that value. If 30% of your holdings are in bonds, and you rebalance at exactly the bottom of the stock market, you only need the stock index to increase in value by about 80% from the bottom in order to make you whole again.
I mention those two to emphasize that your investment return is not just a function of the price of a stock index.
Dollar cost average. It's rare that you will actually face the situation of putting (say) $100,000 into the market all at once, let it sit for 10-20 years, then take it all out at once. The situation you face is closer to putting about $1000 into the market every month for 100 months. If you do that, then you're getting a different price for each purchase you make. Your actual return will be a weighted average of the return from each of those purchases. But note that this could help or hurt you. Using the chart Victor showed in his answer, if your lost decade is from one peak to the next peak, your average price will be below the price you would have entered and left at. So this helps. But if your lost decade is from trough to trough, then your average price is higher than the start and end price, so this has hurt you. Those are the two extreme cases, and the general case will be somewhere in between. And you can use these regular purchases to help you carry out your regular rebalancing.
Foreign equities. Since you mention the S&P500 specifically, I assume that you are in the United States. The US equities is approximately 45% of the world equities market. So even if the S&P500 has a lost decade, it's unlikely that the rest of the world will also have a lost decade at the same time. For comparison, the Tokyo Stock Exchange is the third largest in the world (behind the US's NYSE and NASDAQ); the market cap of the TSE is less than 20% that of the combined market cap of the NYSE and the NASDAQ, which puts it at about 10% of the world's market cap. When the Nikkei had its lost decades, no one else had a lost decade. Note that buying foreign equities is more expensive than buying domestic, and it exposes you to fluctuations in the exchange rate of the currencies. But the benefit of diversification probably outweighs those downsides. And obviously it's easier to diversify away from Japan than it is to diversify away from the United States. But there are people who advocate holding exactly the market weight of every country in the world.