The phrase, "give them what for", is an expression that was used much more during the late 1800's and up through the mid-1900's. Like so many "slang" terms, phrases & expressions, they become popular with a particular generation, however, as that generation gets older and begins to perish, the usage of that phrase becomes more & more obsolete. That's not to say that the expression is never used, but the individuals who use the term today, let alone understand the meaning, are few and far in between.
The expression, "Give them what for" or simply, "what for", means to punish, reprimand, castigate, etc.
If someone told u that u were going to be punished, your natural response would be, "what for" a.k.a. (the reason for punishment). There's always a reason for a punishment. When someone or something is punished for NO REASON, that's called malevolent, cruel & barbaric (rotten to the core).
So, to properly use the expression, "give them what for", the person(s), that's being punished, must be aware of their wrong-doing and/or mistake. The perpetrator doesn't need to be aware of the upcoming punishment or who's going to deliver the punishment. For example, if you & a friend witnessed an elderly woman being mugged, and you were close enough to help the woman, you might say to your friend, "Let's go give that prick some what for!" (sorry about corny example)
Hope that helps, at least little bit.