WASHINGTON—The House defeated a Republican-written farm bill on Friday after GOP leaders couldn’t win over a bloc of their party’s most conservative lawmakers, who were demanding a separate vote soon on a measure to toughen immigration enforcement.

The conservatives, members of the House Freedom Caucus, broke with their party even as leaders tried to persuade them that an immigration measure they wanted to bring to the floor would come up in June as part of a series of other immigration votes. The group instead wanted an immediate...