Answer: Dunn wasn't from around there, and Elfreth had a lot of outside help
Meredith, the race seemed very close in a way that made it hard to project just who would win. It's true that Dunn raised a very large amount of money and definitely had a lot of traction online. But Elfreth raised a fair bit herself and actually represents part of the district in the Maryland state legislature, whereas Dunn didn't have an initial base of support as he hails from outside the seat in Montgomery County. Most critically, Elfreth received ample outside support from United Democracy Project, the AIPAC-associated super PAC. So while Dunn had raised $4.6 million to Elfreth's $1.5 million, UDP spent $4.2 million in support of Elfreth while Dunn had no outside help. To be clear, a candidate's campaign dollar goes farther when booking ads than for outside groups, but as Jacob pointed out in late April, the combined broadcast ad time bought by Elfreth and UDP sometimes outdistanced Dunn during the campaign.
—Geoffrey Skelley, 538