blipping back into endgame

i know it’s been 2 years since endgame came out but this still confuses me. during the final battle, how did doctor strange know to portal everyone in ? how was everyone ready to fight even though they were just brought back after 5 years of being gone ?