Matthew relates that "in the days of Herod the king, magi from the east arrived in Jerusalem" (Mt 2:1) looking for Jesus. Herod had heard they were following a star and enquired of Jewish officials "where the Messiah was to be born" (:4). They quoted "from you [Bethlehem] One will go forth for Me to be ruler in Israel" (Mic 5:2). Herod sent them there to search (Mt 2:8) and to report back. They found Jesus and "presented to Him gifts" (:11) but "having been warned by God in a dream not to return to Herod, the magi left for their own country by another way" (:12).
Researchers have discovered that if people are repeatedly woken up while dreaming it has detrimental effects. There is a psychological purpose. However, dreams can also be instructional albeit frustrating when interpreting them. But when God gives a dream the interpretation accompanies it. Even so, others may not accept the explanation. Joseph asked his brothers to, "'Please listen to this dream which I have had'" (Ge 37:6). But "they hated him even more for his dreams and for his words" (:8). A Godly dream can save your life. "An angel of the Lord appeared to Joseph in a dream" (Mt 2:13) and told him to take the family to Egypt. He said Herod would try to kill Jesus and they were to stay in Egypt temporarily (:13). Paul instructed to "live by the Spirit" (Gal 5:25), be "led by the Spirit" (:18), and "walk by the Spirit" (:16). The angel said, "'Remain there until I tell you'" (Mt 2:13). Obedience is essential. "Joseph got up and took the Child and His mother while it was still night, and left for Egypt" (:14).
"When Herod died, behold, an angel of the Lord appeared in a dream to Joseph in Egypt" (Mt 2:19). He was instructed to return to Israel (:20). Matthew cites the prophecy of this (:15). "When Israel was a youth I loved him, and out of Egypt I called My Son" (Hosea 11:1). God told Moses, "'And it shall be when your son asks you in time to come, saying, 'What is this?' then you shall say to him, 'With a powerful hand the Lord brought us out of Egypt, from the house of slavery'" (Ex 13:14). God had "led them" (Hos 11:4), "fed them" (:4), and "healed them" (:3). But "they kept . . . burning incense to idols" (:2) and "they refused to return to Me" (:5). Then again "after being warned by God in a dream, he [Joseph] left for the regions of Galilee, and came and lived in a city called Nazareth" (Mt 2:23) fulfilling the prophecy "'He shall be called a Nazarene'" (:23).