With Mary so close to delivering her child, why did she and Joseph risk traveling to Bethlehem? Taxes.
A census was ordered to determine the taxes due from the residents of the Roman Empire. Each person had to return to his home town to meet the decree's requirements.
So Joseph and Mary headed out from Nazareth in Galilee to Judea, specifically to the city of David known as Bethlehem, an estimated three-day trip, because Joseph was of the house and family of David.
And the rest is Biblical history.
Here's the story from the Gospel of Luke, Chapter 2:
1 In those days a decree went out from Caesar Augustus that the whole world should be enrolled.
2 This was the first enrollment, when Quirinius was governor of Syria.
3 So all went to be enrolled, each to his own town.
4 And Joseph too went up from Galilee from the town of Nazareth to Judea, to the city of David that is called Bethlehem, because he was of the house and family of David,
5 to be enrolled with Mary, his betrothed, who was with child.
6 While they were there, the time came for her to have her child,
7 and she gave birth to her firstborn son. She wrapped him in swaddling clothes and laid him in a manger, because there was no room for them in the inn.
8 Now there were shepherds in that region living in the fields and keeping the night watch over their flock.
9 The angel of the Lord appeared to them and the glory of the Lord shone around them, and they were struck with great fear.
10 The angel said to them, “Do not be afraid; for behold, I proclaim to you good news of great joy that will be for all the people.
11 For today in the city of David a savior has been born for you who is Messiah and Lord.
12 And this will be a sign for you: you will find an infant wrapped in swaddling clothes and lying in a manger."
13 And suddenly there was a multitude of the heavenly host with the angel, praising God and saying:
14 "Glory to God in the highest and on earth peace to those on whom his favor rests."