Matthew 2:1
Now when Jesus as born in Bethlehem of Judaea in the days of Herod the king, behold, there came wise men fom the east of Jerusalem...
2. Saying, Where is he that is born King of the Jews, for we have seen his star in the east, and have come to worship him....
3. When Herod the king had heard these things, he was troubled, and all Jerusalem with him.
4. And when he had gathered all the chief priests and scribes of the people together, he demanded of them where Christ should be born.
5. And they said unto him, it is written by the prophets, that he should be born in bethlehem of Judaea, for thus have they said.
7. Then Herod, when he had privately called the wise men, enquired of them diligently what time the star appeared.
8. And he sent them to Bethlehem, and said, Go and search diligently for the young child; and when ye have found him, bring me word again, that I may come and worship him also [ Herod is lying and he secretly wants to kill the child.]
9. When they had heard the king, they departed; and, lo, the star which they saw in the east, went before them, till it came and stood over where the young child was.
10. When they saw the star, they rejoiced with exceedingly great joy.
11. And when they came into the house, they saw the young child with Mary his mother, and fell down, and worshipped him. They presented unto him the gifts of frankincense, gold, and myrrh. These costly gifts helped pay expenses for the trip to Egypt.
12. Being warned of God in a dream that they should not return to Herod, they departed into their own country another way.
13. And when they were departed, behold, the angel of the Lord appeared to Joseph in a dream, saying, Arise, and take the young child and his mother, and flee into Egypt, and be thou there until I bring thee word: for Herod will seek the young child to destroy him.
14. When he arose, he took the young child and his mother by night, and departed into Egypt.
[this would be a long and difficult journey, of about 200-300 miles, depending on where they stayed in Egypt, but Joseph and Mary were obedient].
15. And was there until the death of Herod.
By the time the wise men arrived in Jerusalem, enquiring about the birth of Christ, Jesus was already a young child. Joseph and Mary had apparently decided to stay in Bethlehem for awhile, after the Savior's birth, and had settled in a house there. You can see that this implied in verse ll, which informs us that He was now living in a house in Bethlehem. Thus, the pictures we see of the wise men and shepherds visiting baby Jesus in a stable are a composite of two separate events.