"He is giving a teacher an apple."
Translation:Er gibt einer Lehrerin einen Apfel.
I wrote 'Er gibt einen Apfel einer Lehrerin' and it was incorrect. Does the word order "have to" be like this?
Pretty much, yes -- if the direct object and the indirect object are both nouns or noun phrases, the indirect object comes first.
Both masculine dative einem Lehrer and feminine dative einer Lehrerin are accepted as translations for free-type exercises.
I wrote "Einer lehrerin gibt er einen Apfel" and it wasn't accepted. Am I missing something here?
Your sentence is fine. Uncommon order, but still common enough to hear it every day in germany.
Duolingo wants you to translate in accordance with their word order. That doesn't necessarily mean your sentence is wrong, sometimes it is just different.