There is a tradition of consecrating one’s virginity to God. In Mary’s time, I believe there were consecrated “temple virgins” associated with the Jewish temple, and pious tradition says that Mary was one of these. The temple virgins were young girls under age 14 who prayed and assisted with tasks at the temple such as sewing, weaving, and laundering liturgical linens. They were also educated by some older women in charge of the girls. The girls were dismissed at age 14. This did not require a monastery or nunnery.
Whether she was consecrated to God or not, before Mary was married, she would have been expected under the Jewish law of the time to stay a virgin until she married.
After she married, her husband Joseph would have needed to agree to allow her to remain a virgin while also being his wife. He obviously agreed to this.
Mary did not have “children” by the way, she had one child, Jesus, and his father was God, not Joseph. Joseph was his foster father and protector.