There's a few gaps in your assumptions that can give Euron a window to do what he does.
Note that I'm forgoing spoiler tags as this entire answer is one big spoiler.
We see Euron's Iron fleet wreck Yara and Theon's Iron fleet. I assume this happened between Dragonstone and King's landing, but could be mistaken.
Looking at the map you posted, there's no reason for Yara to sail towards King's Landing (Blackwater Bay in general).
Although this is an assumption on my part, the fact that the Sand Snakes were cozy in their hammocks and Yara was having a drink with Ellaria, it stands to reason that they had been travelling some time. So I doubt that the ambush took place near Blackwater Bay.
However, looking at the route from Dragonstone to Sunspear (their intended destination), it seems there are a few locations that are hard to avoid. Keep in mind that it's significantly easier to sail by staying close to shore (visual aid), rather than having to rely on compasses or astronomy.
Euron could have been waiting for them at one of the unavoidable points, e.g. the entrance to the sea of Dorne, or the eastern cape just south of the sea of Dorne. Any route that avoid these points will be incredibly convoluted, and it seems clear that Yara wasn't expecting hostile interaction on the way (so it makes no sense for her to take such a convoluted route then)
How did he manage to get from one side of Westeros to the other so quickly, assuming as it is portrayed on the show that both of the fleets under Dany's command left at the same or similar times.
It would be more accurate to say that Grey Worm and Yara's fleets started preparing for their journey at the same time.
Looking at the map once again, if Yara and Grey Worm left at the same time, they must have been travelling together, since Sunspear is on the way to Casterly Rock.
Yara's mission was to fetch Dornish soldiers. Logically, she must have left with relatively empty ships, so that the Dornish can be added (without needing to leave others behind, which would defeat the purpose). That requires little preparation time.
Grey Worm's mission was to send soldiers. This means that a lot more preparation time is needed. Provisioning the boats, loading the men, ...
It also seems fair to consider that the Greyjoys are the most adept sailors compared to everyone else, so it's very likely that they can mobilize faster than others. This can apply to both Yara and Euron.
New conclusions
Let me rewrite your interpretation according to the differences I've addressed.
- Yara leaves before Grey Worm, due to less preparation time and having worthy sailors as a crew.
- Euron waits for Yara at given location and ambushes her.
- Euron sends his fleet to Casterly Rock (he himself took a trip to King's Landing if you remember)
There are two options here:
- Euron's fleet could have left for Casterly Rock before Grey Worm. This makes it likely that they were again waiting to ambush Grey Worm, and were even there before him.
- Maybe Grey Worm had already progressed further when Euron's fleet departed for the Rock, but since Euron ambushed Yara somewhere between Dragonstone and Sunspear (which is en route to Casterly Rock), then Euron's fleet had less distance to travel than Grey Worm did. If you combine the Greyjoy sailing aptitude, shorter travel distance, and Grey Worm's time needed to take Casterly Rock after their sailing trip; then this should give Euron's fleet more than enough time to catch up.
Regardless of the options, I think the relatively unmentioned but important preparation time for Grey Worm's fleet to depart gave Euron enough time to attack both fleets.