The S&P 500 staged a dramatic reversal in the final hour of trading to close slightly higher — after briefly dipping into bear market territory earlier in the day.