For those unfamiliar with the expression, this idiom can be used to gloat (as Matt Damon's character did in "Good Will Hunting") or to call attention to a small personal triumph. It's sort of another way of saying "So there" (but with apples).
OK, but how did the expression get its start? According to a few sources, it originated in World War I with the "toffee apple," a kind of trench mortar bomb sometimes used to destroy tanks. We presume soldiers would say, "How you like them apples?" after an "apple" took out an enemy.
A character played by Walter Brennan used the phrase in the John Wayne movie "Rio Bravo." After throwing a stick of dynamite and seeing his partner shoot it in the air, Brennan shouted, "How do ya like them apples?"
Ironically, whether in the movies or on the battlefield, the person being asked usually isn't around to answer the question.