When. At the threshold, as you step in — many say it aloud, then greet whoever is inside with salam. It belongs to the small hinge moments of the day rather than to any appointed hour.
The words. Walajna and kharajna — "we entered", "we left" — are both plural and both in the perfect tense, so a man arriving alone still speaks as one of a household. The two halves are symmetrical on purpose: the entering sentence already contains the leaving. Bismillahi is repeated rather than carried over, which in Arabic keeps the second clause standing on its own feet. In the final clause, 'ala Rabbina is placed before the verb tawakkalna, and that fronting narrows the meaning: not merely that we rely on our Lord, but that it is upon Him, and no other, that we rely.
Why a doorway. The home is where a person stops performing. Entering it, the supplication attaches the name of Allah to the exact place where vigilance drops, and it does so for both directions of travel at once — the return is asked for in the same breath as the departure.
The household. The greeting of peace on entering is a separate and well-known practice, and the two sit naturally together: the name of Allah at the door, then salam to the people behind it.
Common confusion. This is not the supplication said on leaving the house, which begins with reliance and asks for protection from misstep. They are distinct texts, and saying one does not stand in for the other.