The S&P 500 staged a late-day rally, extending its weekly advance, with energy and financial companies among the biggest gainers.