US stocks rally late to finish July with robust month of gains