The story
From 19th century, as a threatening rebuke to an impertinent (by 19th century standards) question of "what for?". Parent: "Go to your room." Child: "What for?" Parent (angrily): "Disobey me, and I'll give you 'what for'!"
Etymology from Wiktionary, CC BY-SA 4.0.