Translation:He always opens the window when he goes to sleep.
Judging by the translation I guess "Ele sempre abre a janela quando vai para dormir" would be more appropriate.
To go to sleep = Ir dormir. There's no preposition. The "to" here is just used for the infinitive form of both verbs in English. So you simply use the equivalent infinitive forms in Portuguese.
I got what you mean but vai dormir also means "be going to sleep" or "will sleep" as long as I remember. But duolingo doesn't allow that translation. What do you think? I'm not sure since neither portguese not english isn't mother-tongue.