Foster's election is a bad sign for Republican congressional candidates in November, political analysts said. Foster won with 52 percent of the vote to 48 percent for Republican Jim Oberweis in what had been considered a safe Republican district, the Chicago Tribune reported.
Oberweis and Foster engaged in a heated race, with the candidates' parties shelling out big bucks to boost their nominees' chances.
Hastert, who served as House speaker from 1999 to 2006, decided not to run for minority leader when Republicans lost control of the House. Plagued by a series of party scandals, he resigned his seat last year.
Oberweis, a dairyman and investor, has run unsuccessfully in three previous bids for governor and for the U.S. Senate in 2002 and 2004. Foster, a Harvard-educated physicist, co-founded a large theater lighting company.
The district includes Kane and Kendall counties, and portions of six others, including western DuPage and DeKalb.
Foster will serve the remainder of Hastert's term and can now run as an incumbent in November.
Rep. Chris Van Hollen of Maryland, chairman of the Democratic Congressional Campaign Committee, said if Foster wins in the "rock-ribbed" Republican district it would "send a shock wave through the political system that people are absolutely fed up with the status quo."