All perfect praise be to Allah, The Lord of the worlds. I testify that there is none worthy of worship except Allah and that Muhammad, sallallahu ‘alayhi wa sallam, is His slave and Messenger.
Seeking protection of Allah when going out is done by saying the supplication upon leaving your own home, such as your apartment (flat), for instance.
Anas ibn Maalik, may Allah be pleased with him, narrated that the Prophet, sallallahu ‘alayhi wa sallam, said:
"If a man leaves his house and says, 'Bismillah, tawakkaltu ‘ala Allah, wa la hawla wa la quwwata illa billah.' (In the Name of Allah; I rely on Allah; there is no power nor might save in Allah), then it will be said to him, 'You have been guided, sufficed, and protected.' The devils will move out of his way, and one devil will say to another, 'How can you have access to a man who has been guided, sufficed, and protected?'" [Abu Daawood - Al-Albaani graded it saheeh (sound)]
A person who lives in a mansion or the like should say the supplication upon exiting the first door (the house door), not the yard gate.
A guest should say the supplication when leaving the host's house. An-Nawawi said:
"You should know that it is recommended to recite the Basmalah (saying 'Bismillah', which means: in the Name of Allah) upon entering one's house or the house of others, to say Salaam (greeting of peace) even if there is no one inside, and to say the supplication upon going out. Anas, may Allah be pleased with him, narrated that the Prophet, sallallahu ‘alayhi wa sallam, said, 'Whoever says (meaning if he goes out of his house), 'Bismillah, tawakkaltu ‘ala Allah, wa la hawla wa la quwwata illa billah,' it will be said to him, 'You have been defended and protected,' and the Devil will move out of his way.' [Abu Daawood and At-Tirmithi who graded is hasan (good)]."
Allah knows best.