Wh- words like which, whom and why get a lot of knickers in a twist, as attested by this oatmeal comic on when to use who vs whom, or the age-old debate about the correct use of which vs that (on which see this blog post by Geoffrey Pullum). But in Old English the wh- words formed a complete and regular system which would have been easy to get the hang of. They were used strictly as interrogative pronouns – words that we use for asking questions like who ate all the pies? – rather than relative pronouns, which give extra information about an item in the sentence (Jane, who ate all the pies, is a prolific blogger) or narrow down the reference of a noun (women who eat pies are prolific bloggers). They developed their modern relative use in Middle English, via reinterpretation of indirect questions – in other words, sentences like she asked who ate all the pies, containing the question who ate all the pies?, served as the template for new sentences like she knew who ate all the pies, where who functions as a relative.
Originally, the new relative pronoun whom (in its Middle English form hwām) functioned as the dative case form of who, used when the person in question is the indirect object of a verb or after prepositions like for. For direct objects, the accusative form hwone was used instead. So to early Middle English ears, the man for whom I baked a pie would be fine, while the man whom I baked in a pie would be objectionable (on grammatical as well as ethical grounds). Because nouns also had distinct nominative, dative and accusative forms, the wh- words would have posed no special difficulty for speakers. But as English lost distinct case forms for nouns, the pronoun system was also simplified, and the originally dative forms started to replace accusative forms, just as who is now replacing whom. This created a two-way opposition between subject and non-subject which is best preserved in our system of personal pronouns: we say he/she/they baked a pie, but I baked him/her/them (in) a pie.
Thus hwone disappeared the way of hine, the old accusative form of he. Without the support of a fully-functioning case system in the nouns, other case forms of pronouns were reinterpreted. Genitive pronouns like my and his were transformed into possessive adjectives (his pie is equivalent to the pie of him, but you can no longer say things like I thought his to mean ‘I thought of him’). The wh- words also used to have an instrumental case form, hwȳ, meaning ‘by/through what?’, which became an autonomous word why.
Although him and them are still going strong, whom has been experiencing a steady decline. Defenders of ‘whom’ will tell you that the rule for deciding whether to use who or whom is exactly the same as that for he and him, but outside the most formal English, whom is now mainly confined to fixed phrases like ‘to whom it may concern’. For many speakers, though, it has swapped its syntactic function for a sociolinguistic one by becoming merely a ‘posh’ variant of who: in the words of James Harding, creator of the ‘Whom’ Appreciation Society, “those who abandon ‘whom’ too soon will regret it when they next find themselves in need of sounding like a butler.”