This story is called The Phantom Tollbooth written by Norton Juster. The story starts with a boy named Milo that receives a package that contains a magical tollbooth and a car.
The tollbooth sends him to a magical land where Milo stumbles upon a place called the doldrums. In the doldrums Milo meets a watchdog named Tock, who agrees to accompany Milo to help him escape the doldrums.
Then Milo travels to Dictionopolis and Digitopolis, where he meets King Azaz and the Mathmagician. They tell him that he needs to save the two princesses that are trapped in the Castle in the Air, Milo agrees to help. A bug named Humbug also agrees to accompany Milo.
Next on Milo's journey he and his friends are attacked by demons, that chase them all the way to the castle.
When Milo got to the castle he used gifts that Azaz and the Mathmagician gave to him to free the princesses.
When Milo brought back the princesses he and his friends were congratulated and Milo went back to his own world.