The 218-209 vote Wednesday would undo a program President Barack Obama announced in 2012 called Deferred Action for Childhood Arrivals.
The program has extended work permits and temporary protection from deportations to more than 600,000 immigrants who arrived in the U.S. illegally before they turned 16 and who meet other criteria.
Some moderates and Republicans with heavily Latino districts opposed the legislation and more than two dozen joined Democrats in voting no.
The vote came on an amendment to a spending bill for the Department of Homeland Security. Obama has threatened to veto the legislation.