Financials and utilities were the day's biggest gainers, with the S&P financial sector index up 1.3% and the utilities sector index up 1.6%.
On the Nasdaq, Yahoo! Inc fell 6.6% to $34.07 after Alibaba Group filed for an initial public offering that valued the Chinese e-commerce company well below analysts' consensus estimate.
Analysts at BGC Partners and Atlantic Equities told Reuters that Alibaba's valuation disappointed some Yahoo investors. Yahoo holds a 22.6% stake in Alibaba, of which it must sell more than a third through the IPO.
"The Alibaba IPO is drawing money out of Internet stocks because investors have to raise capital for the Alibaba IPO. That's why we see pressure on names like Google, Facebook and Amazon today," said Tim Ghriskey, chief investment officer of Solaris Group in Bedford Hills, New York.
Google shares fell 1% to $509.96 while Facebook shares slid 2% at $57.39. Amazon.com Inc declined 1.6% to $292.71.
Groupon Inc sank 20.7% to end at $5.33 as its sales and profit projections for the current quarter trailed some estimates.
Twitter Inc lost 3.7% to close at $30.66 after leading a selloff in Internet shares on Tuesday with an 18% tumble.
The Dow Jones industrial average rose 117.52 points or 0.72%, to close at 16,518.54. The S&P 500 gained 10.49 points or 0.56%, to end at 1,878.21. The Nasdaq Composite dropped 13.085 points or 0.32%, to finish at 4,067.673.
After declining in the morning session, the Dow and the S&P 500 moved into positive territory after Yellen said the US economy was still in need of lots of support, given the "considerable slack" in the labour market in remarks to the congressional Joint Economic Committee.
Yellen's comments enabled investors to shift attention to what may be an easing of the tensions in Ukraine, as Russian President Vladimir Putin called on pro-Moscow separatists in Ukraine to postpone a vote on secession just five days before it was to be held.
"Janet Yellen didn't say anything new in her testimony. She continues to talk about how they are data dependent, how they can change on a dime, but she didn't say rates are going up any time soon, which is exactly what people expected," said Ken Polcari, director of the NYSE floor division at O'Neil Securities in New York.
"Coupled with the fact that technically the market found some support, then (investors) started concentrating on what Putin said," Polcari added.
Whole Foods Market Inc also contributed to the Nasdaq's decline as the organic grocer's stock tumbled nearly 19% to $38.93.
The stock was the biggest drag on both the S&P 500 and Nasdaq 100 indexes a day after Whole Foods posted second-quarter results and cut its 2014 outlook.
Of 422 companies in the S&P 500 that had reported earnings through Wednesday morning, 68.2% beat expectations, above the 63% average since 1994, and exceeding the 66% beat rate for the past four quarters, according to Thomson Reuters data.
Profits are expected to rise 5% this quarter, down from 6.5% estimated at the start of the year, but above the low of 0.6% in mid-April, according to Thomson Reuters data.
In the latest snapshot of the US economy, the Labor Department said that US nonfarm productivity fell at its fastest pace in a year in the first quarter because of severe weather. That led to the largest gain in unit labour costs in more than a year. Productivity fell at a 1.7% annual rate in the quarter, the Labor Department said.
About 7 billion shares changed hands on US exchanges, well above the 6.1 billion average over the past five days, according to data from BATS Global Markets.
Advancers outnumbered decliners on the New York Stock Exchange by a ratio of nearly 2 to 1. On the Nasdaq, however, about five stocks fell for every four that rose.