Simple answer: blind partisanship with a heaping side of anti-abortion.
King represents the northwest part of Iowa. This is by far the most conservative (and sadly least educated) part of Iowa. It's basically Iowa's Alabama. The only solidly liberal part of King's district is Story County, home to Iowa State University. It is on the SE edge of the district. Move the district line one country and King's district is as red as it gets.
This district has a high concentration of evangelicals and other ultra-conservative religious people. They hate abortion. Abortion is the single greatest factor in their votes. King hates abortion, too. The GOP is anti-abortion. That's all they need to know.
King hates illegal immigrants. Ironically, this district is chock full of farms that depend on illegal immigrant labor, especially dairy farms. But... King is a Republican who hates abortion, so the farmers shrug and vote for him anyway.
But, but, King's a racist. WTF? This is Fox :"News" country, and AM radio talk shows are hugely popular. On Fox and talk radio, King being a racist is fake news. It's all a giant smear campaign by liberals. But, "Republicans condemned King, too," you say. "Oh, well, it's all a giant smear campaign by liberals and never-Trumpers then. We know Steve King, and he's a fine man."
Someone above suggested King must really serve his district well. Strangely, that's not really true. Other than being an anti-abortion Republican, King's done almost nothing for his district. He's introduced something like 100 bills to the House, yet the only one that ever made it out of committee was one to rename a post office. That's it. He's been there 16 years IIRC, and passed exactly one bill. Sad.
Someone else suggested his opponent (J.D. Scholten) did a horrible job campaigning. Quite the opposite. The only reason Scholten made this such a close race is because he ran an extraordinary campaign. Probably the best the district has ever seen. Note the King usually wins with solid double-digit margins. The closest anyone got before Scholten was losing by 8 points.
Finally, all of the above notwithstanding, these election results are a bit strange. The other three Iowa House seats all went blue. The Democratic challenger for governor (Hubbell) had a substantial lead all night long... until the last few results came in from King's district. Everyone expected Hubell's lead would fall a bit due to demographics. But, this was over the top. Mathematically, it is only possible if an unusual number of Iowans voted split tickets, i.e, blue for Congress but red for governor. That's certainly possible, but odd. It is being investigated.
Thank you for that. It's pure tribalism, isn't it? Even after Trump set up Iowa soybean farmers for the Chinese nut kick they still believe...