Shoe has already answered. Here is some additional information that may or may not clarify things further.
The first important thing to understand is that the relative pronoun is part of the relative clause, not of the main clause. Recently, even some native English speakers seem to be getting confused about this, as some weird new practices around who/whom show.
Let's look at some examples around the main clause "The man is here." I will put the relative clauses in bold face.
- The man who wrote the message is here.
- The man whom I wrote the message is here. [questionable example; see below]
- The man to whom I wrote the message is here.
- The man whom I wrote the message to is here.
We can see that in all examples the relative clause immediately follows the part of speech (here the noun man) which it defines or explains; the remainder of the main clause follows after the end of the relative clause. This is not absolutely necessary, but it's the normal order. (One could also say "The man is here who wrote the message", but that's unusual.)
Next we see that in all examples the relative pronoun (in this case who[m]) begins the relative clause, possibly together with a preposition. This can be explained as one of the few remnants that English has from the original proto-Germanic word order: Word order was generally very free, and the sentence started with the semantically most important part of speech. In a relative clause that's the relative pronoun -- where applicable with its preposition. (It's a general phenomenon in Germanic languages that the word order in subordinate clauses is more conservative than that in declarative main clauses.) As a special feature of English, the preposition can optionally be separated and 'stranded' at the end of the sentence (or here: of the relative clause) as in the last example.
If we replace who/whom by he/him and write all the relative clauses as if they were main clauses, we get declarative main clauses with an unusual (almost obsolete) word order that puts enormous stress on [to] him. The only exception is the first example, where the word order happens to be the normal one because it's the subject that needs stressing.
- He wrote the message.
- Him I wrote the message.
- To him I wrote the message.
- Him I wrote the message to.
Preposition stranding is a normal but optional and somewhat controversial feature of English. (Controversial among native speakers, that is. Non-native speakers get drills in preposition stranding so they can apply it properly.) I can see two problems with it. 1. It defers the information about the precise role of the relative pronoun in the relative clause to the very end of the relative clause. 2. There are transitions between prepositions and case markings. In fact, English to and of, just like French de and à, have developed to a stage where they can be considered as case markers. (Most case markers in European languages were once postpositions and therefore turned into endings. But prefixes marking case aren't unheard-of, either.) Seen this way, preposition stranding (in case of to or of) separates a relative pronoun from its case marker. That's like separating the -m from whom and 'stranding' it at the end of the sentence.