After listening to the king, they [the Magi] went on their way. And behold, the star that they had seen when it rose went before them until it came to rest over the place where the child was. When they saw the star, they rejoiced exceedingly with great joy. (Matthew 2:9-10)The star that the Magi saw had some rather strange qualities. No ordinary heavenly body would have had the ability to "go before them" and then come "to rest over" a certain spot. The star that the Magi saw, however, did have these abilities. It would be best, then, to interpret this star as something supernatural— something put into place by God Himself for a certain purpose, for a certain people, and a at a certain time— something that would not fit into the modern, narrower definition of a star (for more info on the nature of the star see Matthew 2:1-2).
When the Magi saw the star they rejoiced exceedingly with great joy. This small statement seems to imply that the star, which the Magi had so curiously observed, disappeared sometime during their journey to Jerusalem. When the star finally reappeared, the Magi rejoiced at the sight.
If we assume that the Magi did lose the star for some time, this naturally leads to the question: Why? Assuming that the star was of entirely supernatural origin, this question becomes even deeper: why did God choose to hide the star that He had been using to guide the Magi? This is a question that delves into the very purposes and plans of God.
One possibility is that God hid the star from the Magi so that they would have to go to Jerusalem (and not directly to Bethelehem). While in Jerusalem, the Magi alerted Herod concerning the birth of the King of Kings. Without this key component of history, Jesus' family would have had no reason to flee from Bethlehem and into Egypt— and if Jesus had never entered Egypt, the prophecy that He would be called up out of Egypt would not have been fulfilled (Matthew 2:15).
The star likely reappeared after the Magi left Jerusalem. The supernatural sight brought joy to the Magi and also confirmed that Herod had spoke the truth when he said that the Child was in Bethlehem (Matthew 2:8). They Magi now knew where they were going, and they had a divine guide leading them there— who wouldn't rejoice exceedingly with great joy?