U.S. stocks ended the day higher in the final trading session of July, marking the best monthly gain for the S&P 500 since August of 2020.