I posted the second answer in this Scifi.SE question. It's very similar to your question. My answer here rehashes the same information, but it tries to answer your question directly.
Meanwhile, Euron Greyjoy is leaving from King's Landing for some unknown destination.
If anything, it seems that Euron's fleet had a longer way (starting from King's Landing) and was delayed by the battle. Depending on whether Euron himself is actually leading the fleet, he also took another detour back to King's Landing to parade his prisoners.
Just because Euron visits King's Landing, does not mean that Euron's fleet is there too.
A chronological turn of events:
- Yara travels from Dragonstone to Sunspear. (which is on the way to Casterly Rock)
- Yara gets attacked by Euron. This takes places somewhere between Dragonstone and Sunspear. Though we don't know the exact location, we do know that it's closer to Sunspear (and by extension Casterly Rock) than King's Landing and Dragonstone are.
- Euron shows up in King's Landing with his present for Cersei. We don't know where the fleet is.
- Euron's fleet shows up at Casterly Rock. We have no idea where Euron is.
You did mention that Euron could be somewhere else than his fleet, but you then continue with the assumption that his fleet must be leaving from King's Landing.
That makes little sense. If Euron and his fleet split up, why would his fleet then return to King's Landing after attacking Yara's fleet?
So what happened here? I was thinking maybe he had the better ships, but I seem to remember that Yara and Theon took the best ships of the Greyjoy fleet. (Though that might have been book-only.)
There are some things I can mention here; not all of them have been discussed by the show.
- You're right that the Greyjoys (Yara and Euron) can be assumed to travel faster than others, due to their seaworthy nature.
- (only in the books) Euron dabbles with scary magic, he is a very lovecraftian character. Though this may be a matter of appearing scary (without any real magical aptitude), it could also justify why Euron can sail even faster, located Yara's fleet easily, and maybe even conjure up the mist from which his ships appeared after sneaking up on Yara's fleet. However, there is no confirmation in the show whatsoever, although his character does seem evil in a way that would suggest that he has no issue with using dark magic.
- When you question the timing of the events at Casterly Rock, you also need to consider at what time Grey Worm's fleet actually left Dragonstone. If Grey Worm left at a later time, that means they arrive at a later time and Euron's fleet has more time to catch up. It is very likely that Grey Worm left Dragonstone much later than Yara. Yara's mission was to fetch Dornish soldiers. Her ships were running on a skeleton crew, in order to pick up as many soldiers as possible. Grey Worm's mission was to transport soldiers to Casterly Rock. This requires much more logistical effort: loading the men, provisioning the ships (for a longer journey), loading weapons and armaments, ...
Grey Worm is at a clear time disadvantage here. Even if his fleet managed to leave Dragonstone before Euron's fleet attacked Yara's fleet, he still has to travel further than Euron's fleet (which is already somewhat on the way to Casterly Rock); plus he also has to spend time taking Casterly Rock after landing.
This gives Euron a considerable window to catch up to Grey Worm and sack his fleet.
You might ask yourself how Grey Worm did not run into Euron's fleet if he ends up arriving at Casterly Rock before Euron's fleet. It's a valid question, but I think that it's perfectly possible that Euron's fleet arrived first.
From what we see of Euron's fleet, they are intent on destroying the Targaryen/Greyjoy fleet (the empty ships), not killing the troops. It's possible that Euron waited near Casterly Rock, saw Grey Worm's fleet arrive and leave their ships, and then attacked the empty fleet.
Although this has not explicitly been said yet; I think that it's Euron's (or Cersei's) intention to first destroy Danaerys' ships, to trap them on Westeros. If they attack Danaerys' forces (while they still have ships), Danaerys can simply sail away and return when it suits her.
Euron/Cersei are first eliminating that possibility, so that Danaerys cannot engage in guerilla hit-and-run tactics.
Another unrelated theory
Someone, I believe it was Tyrion, has mentioned that Euron could have more than one fleet. There is no proof that Euron has more than one fleet, but there is no proof to the contrary either.
This is mentioned in the top answer on the Scifi.SE question:
In episode 3, Tyrion says that Euron might have fleets in multiple locations, when Daenerys suggests trying to fight them with dragons. That is the foreshadowing. After that the attack begins in Casterly Rock. We also see again from the map that Pyke is relatively close to Casterly Rock, so the Ironborn can make a second fleet and send it to Casterly Rock before the Unsullied arrive.
I'm not a fan of this theory, but I can't disprove it either.