This is the English translation of an old German answer given by the user splattne in 2011.
The word "weswegen" was not topic of the original German question, but it fits into the same scheme.
There is no semantic difference in today's German between the three interrogative adverbs (more precisely: causal interrogative adverbs) warum, weshalb and wieso. The three words are therefore synonyms and thus arbitrarily interchangeable.
If one is very subtle, one could find the following distinction in the origin of the three words:
warum as a question about the reason or motive
Warum freut sich die Frau?
Weil sie im Lotto gewonnen hat.
Why is the woman happy?
Because she won the lottery.
wieso as a question about the cause
Wieso fällt ein Apfel auf den Boden?
Weil er der Schwerkraft ausgesetzt ist.
Why does an apple fall to the ground?
Because it is exposed to the force of gravity.
weshalb as a question about the purpose
Weshalb besucht er die Schule?
Weil er etwas lernen möchte.
Why does he go to school?
Because he wants to learn something.
warum attested in Old High German wār umbe via Middle High German warumbe from wār wo and umbe um. Equated to worum; source: Grimmsches Wörterbuch
Transfer from Latin qui sic? (like this?) → wie so alt. Ital. per che?, modern Ital.: perché? = per che ragione? ("for what reason"); source: Grimmsches Wörterbuch
aus welchem Grunde (for what reason) - merging of the preposition halb(en) (because of) with the prefixed genitive of the pronoun was (what) source: Grimmsches Wörterbuch
However, the boundaries between the terms are often blurred and in everyday language this distinction is hardly noticed.
As far as usage in publications is concerned, Google's Ngram Viewer shows the following distribution in the past and present.
warum is clearly preferred; wieso, in my subjective experience, one hears more often in oral conversations than one finds in written form.