If, after Thor: The Dark World,
Loki is on the throne and Heimdall committed treason,
then how does Thor return to Asgard in Avengers: Age of Ultron?
Science Fiction & Fantasy Stack Exchange is a question and answer site for science fiction and fantasy enthusiasts. It only takes a minute to sign up.Sign up to join this community
The last we saw, Loki was on the throne of Asgard - disguised as Odin. Since neither Thor or Sif have mentioned Odin's disappearance or Loki being in charge of Asgard since the events of Thor: The Dark World (both have appeared in either other movies or episodes of Agents of SHIELD), we can safely assume that Loki is still disguised as Odin.
Since Heimdall was still acting as the Gatekeeper of the Bifrost under Odin, despite having defied Loki when Loki was publicly in charge, removing him now would raise suspicions he is likely trying to avoid.
Additionally, as we see in the first Thor, Heimdall himself is not specifically required to be able to use the Bifrost, just his sword.
So far, it hasn't been mentioned in the Canon how Heimdall was able to obey Thor to open the Bifrost in the Age of Ultron. And, in the next Thor movie, we'll surely get to know that.
Here are the possibilities:
Loki is still waiting for the right time before he'd act to control the realm in his own favor. So, Heimdall is still the gatekeeper of the Asgard.
It was Loki's trap to bring Thor to Asgard to imprison him or kill him or something else. After the events of The Dark World, Thor was presumably on Earth living with Jane. We don't know what happened on Asgard during this period. Maybe, Loki turned the Asgard into hell (Ragnarok). So, Loki was just waiting for the call from Thor.
Odin took control over Asgard again and Loki was in prison at that point. This is less likely because MCU wants to take the story of Loki being King of Asgard forward, but this is one of the possibilities.