From the Overwatch Wiki
Loot Box's contents are generated when players earn or buy them, not when Loot Box is opened. For example, any Loot Box acquired before installing July 19, 2016 patch will not contain any of Ana's items.
You may need to log in to view the link in the quote. Additionally, the Wiki also states that:
Loot Box items are decided once they are obtained, meaning that you cannot save Loot Boxes until new items (highlight intros, skins, etc.) come out, as you will only get what was available at the time.
To summarize, the loot is determined when the box is earned/purchased. This is likely to discourage hording loot boxes until a special event or new items come out and then opening all your loot boxes in hopes of getting a special event item/new item.