So under Roman rule.....Augustus Caesar sent an order for everyone to register in the village they were from. This is how the trip to Bethlehem came about. Joseph needed to go back to Bethlehem (aka town of David) as he was from the family of David and he took Mary with him.
There was many people in Bethlehem because of the census, so this caused all the inns to be full.....which is how they ended up in a stable.
So Joseph also went up from the town of Nazareth in Galilee to Judea, to Bethlehem the town of David, because he belonged to the house and line of David. He went there to register with Mary, who was pledged to be married to him and was expecting a child. ~Luke 2:4-5