S&P 500 and Nasdaq close at all-time highs, posting their best monthly gains since 2020, fueled by strong earnings and resilient economic growth.