It's unknown but it's assumed they stayed back to help evacuate their soldiers and civilians as that's the last thing you see them doing. They're both strong and capable fighters so I'm sure they must have found a way, or went through the keep after everyone else did.
It is all up to speculation, I think. That's whats fun about it! I was never able to use console commands to look around Helgen and see where everyone was going, but I've found out that Alduin eventually gets the hint and starts attacking just you, if you stay outside long enough. Not very safe. I do know that Ulfric must be outside atleast, since you can hear him shout (possibly at Alduin) before you go into the prisons.
That must mean that someone untied him, and he got his gag off immediately after the attack (upon entering the tower he can talk). Who, we don't know and it's up to speculation! (might be one of his soldiers tho) Tullius was helping fight off Alduin with the other soldiers as well. When you exit from the cave, you also see Alduin fly off, away from the village. That gives people a chance to escape without harm. So Ulfric and Tullius are both outside, which lowers the options, since no one can use the keep as the bridge inside collapsed.
1.) Ulfric slipped out while Tullius hadn't noticed and escaped through the gates. Tullius couldn't find him and had to leave the town (after Alduin left). It's safe to say Tullius stayed until after he left, but with Ulfric it's a bit more tricky; one wrong move on his part and he's captured by imperials again, so he had to be quick and somewhat hidden. That's why he probably escaped while Alduin was attacking since the attack is the distraction to the Imperials, making it easy for Ulfric to run off into the forests or etc.Â
2.) I've heard some talk about how the Thalmor might've helped Ulfric out. If you read his dossier here on the wiki, (spoilers so read at your own risk) it will explain why that might be possible. However it doesn't seem very likely, since Ulfric probably would've  killed any that got close to him. I can't think of how else he would escape though, same with Tullius. That's all I got :L (sorry for the long post btw)
That too, sorry I didn't see what you said till after I posted :pÂ
However the only thing I (respectfully) disagee on is Ulfric staying to help; most of his soldiers were probably killed ("Not many made it out of Helgen") and staying puts him at risk of being captured again, as Tullius doesn't want to let him escape after just getting him. Ulfric probably doesn't want to hang around to find out if anyone would care. Even most of the townsfolk hate him, which puts him at a high risk. I did see Tullius helping citizens and I could see him staying behind to evacuate, though.
I'm not sure about the keep, because if Ulfric went down there it's a dead end (I think), trapping him if any Imperials or Thalmor  followed behind. Then again, this is all my opinion; what happened is unknown and is all up to the imagination.
The soldiers and civilians was a general comment as I was talking about Tullius AND Ulfric. Ulfric at the tower can be see waiting on and helping his soldiers, so there's definite proof that he was at least working on helping them get out before running off himself. Likewise, Tullius can be seen fighting Alduin and Imperial soldiers can be seen trying to evacuate civilians (see Torolf's death) so there's definite proof they were at least working on trying to evacuate the civilians.
Alright, I see your point now. Thanks for explaining it more to me.