Joseph and pregnant Mary arrived in Bethlehem for the census ordered by Quirinius, the new governor of Judea in 6 CE (Luke 2). They couldn't find a place for the night (Luke 2:7) so they stayed in a cave (grotto) below an habitation. This configuration is typical of Israelite habitations in this period, built upon natural or hewn caves to keep storage away from the heat for example. But soon Mary started labour pains, so Joseph went out to seek help from a midwife. Here is how the story is told in the Protoevangelium of James. It is an early Christian writing, dated from about the middle of the 2nd century CE, thus older than the Gospels themselves (hence its name "Proto-"):
And I [Joseph] saw a woman coming down from the hill-country, and she said to me: O man, where are you going? And I said: I am seeking an Hebrew midwife. And she answered and said unto me: Are you of Israel? And I said to her: Yes. And she said: And who is it that is bringing forth in the cave? And I said: A woman betrothed to me. And she said to me: Is she not your wife? And I said to her: It is Mary that was reared in the temple of the Lord, and I obtained her by lot as my wife. And yet she is not my wife, but has conceived of the Holy Spirit.