but the sentence is he wrote her a letter not he wrote a letter to her.
But those sentences mean the exact same thing!
my teacher said to use lui when its "to" someone.
The problem is that English is confusing... because we get to drop the "to" if we put the "someone" earlier in the sentence. Compare:
He wrote a letter to her --> He wrote her a letter.
He gave a present to Jane --> He gave Jane a present; He gave her a present.
etc.
Even though the "to" disappears, moving the "someone" earlier in the sentence doesn't change what actually happened, and it doesn't change the relationship between the different words. What did he write? He wrote a letter. The letter got written. What did he give? He gave a present. The present got given. The letter and the present are called "direct objects" because the action of verb (to write, to give) happens "directly" on them. And regardless of whether we put them earlier or later in the sentence, the words "her" and "Jane" are called "indirect objects" because the action of giving or writing does relate to her/Jane, but only indirectly.
In French, when you need an indirect object pronoun for a single person, you use "lui." It does (generally speaking) correspond to "to someone" in English... but only if you write the English sentence in the version that uses "to"!