What exactly does this phrase mean and in which situations is it used?
It is used as an expression of gloating when someone turns the tables on someone else. There is a good example in the movie Good Will Hunting, where Matt Damon's character (Will Hunting) gets a girl's phone number in a Harvard bar where he, coming from working-class South Boston, is, despite his extraordinary intellect, socio-economically out of his league and is insulted by the Harvard rich kid (Clark) whom he has bested — actually, destroyed — in an argument. On the street later he sees his rival for the girl's attention through a restaurant window. He goes up and raps on the glass to get the young man's attention, and the following dialogue occurs:
Will: Do you like apples?
Will: Well, I got her number. How do you like them apples?
It can also be used as an expression of surprise at a sudden turn of fortune.
Robusto's answer does a good job explaining the meaning of the sentence, but for the sake of completeness, here's the origin of the phrase.
Apparently during the first World War, the Allies had an anti-tank grenade which was colloquially referred to as a "toffee apple" thanks to the appearance of its bulb:
In the John Wayne movie "Rio Bravo", one of the characters launches a "toffee apple" at the enemy lines and says the phrase "How you like them apples?" referring, of course, to the bomb. As movie phrases are wont to do, it entered popular consciousness as a boastful expression of triumph.
I'm pretty sure that the phrase, "how do you like them apples," does not appear in the Wizard of Oz. The apple tree says, "What do you think you're doing!" and "How would you like to have someone come along and pick something off of you."
Rio Bravo was a western - no one shot a "toffee apple" mortar in the movie. If they did use the phras, "how do you like them apples," it would be an anachronism, because the phrase was apparently popularized during World War I.
The British had a trench mortar that was called a "toffee apple." The same mortar was alternately called, "plum pot."
Early in the war, before the "toffee apple" trench mortar was developed, soldiers made improvised explosive devices, grenades and mortars using empty "plum and apple" jam tins. The words "plum" and "apple" may have been associated with mortars and grenades as a result.
Other grenades were also called "apples" during the war, so it was not always a specific reference to the "toffee apple"-style trench mortar.
But even though the expression became popular during the war, there is one known example (as far as I know) of the expression used in Texas in 1895 (also at the above link), so it may have been regional or not widely known before it spread in the trenches of WWI. And its original meaning may not have had anything to do with trench mortars or grenades, even if they played a role in how troops understood or used the expression during the war:
Bryan is the best cotton market in this section of the state and has received more cotton than any other town in this section. How do you like "them apples?"
The Eagle, (Bryan, Texas), September 26, 1895, page 2.
An episode of Perry Mason ("The Case of the Sunbather's Diary" Season 1, Episode 17) has the phrase as "How do you like those potatoes?" A language book from the 1920s uses this version of the phrase in a setting that suggests the 'them potatoes' version was also popular: http://books.google.com/books?id=W9kRAAAAIAAJ&dq=%22how%20do%20you%20like%20those%20potatoes%22&pg=PA43#v=onepage&q=%22how%20do%20you%20like%20those%20potatoes%22&f=false
Looking through Google Books, both 'them/those apples' and 'them/those potatoes' seem to only go back to about the 1920s in print, but the earliest version being a 1919 book of military history where it's listed, without elaboration, amongst humorous stories and sayings. http://books.google.com/books?id=rndBAAAAYAAJ&dq=%22how%20do%20you%20like%20them%20apples%22&pg=PA148#v=onepage&q=%22how%20do%20you%20like%20them%20apples%22&f=false
This suggests the military origin of the phrase to be correct, though in use before it was said in any movie.
As for the meaning -- older versions tend to use it as an expression of surprise, akin to "How 'bout that!" or "I'll be!" Nowadays, however, it's most commonly used as an expression of gloating. (Person #1: "I got 20 points in the game!" Person #2: "Well, I got 200, so how do you like them apples?")