(a) The lining thickens in preparation for a fertilised egg. Were fertilisation to occur, the embryo would need to embed itself in the uterine wall and be nourished there, and a thick, spongy lining richly supplied with blood is what makes this possible.
The preparation is made every month in advance, because the egg is released before it is known whether it will be fertilised.
(b) The process is menstruation. It occurs roughly once a month — about every 28 days — and lasts about two to eight days.
When no fertilised egg arrives, the lining is no longer needed and is slowly broken down and shed, leaving the uterus through the vagina as blood and mucus. A fresh lining then begins to build up for the following month.
So menstruation is not a failure of the system but a routine part of it: the uterus prepares, and if the preparation is not used, it clears it away and prepares again.