Stocks closed with solid gains as Wall Street put the finishing touches on one of its best months of the year.