One would think that this should be directed to an "Ask the Doctor" website, as opposed to "Ask the Rabbi"... But indeed, the Torah encompasses all subjects—and this is no exception.
Our souls existed long before we were born. They lived in a pristine spiritual world and were one with G‑d in every way. Their thinking was not limited to that of a human mind; their seeing was not limited to that of a human eye.
Life in our world is very different. And so, as a baby forms physically, its soul must undergo a "spiritual transformation" before entering this physical world.
Using Kabbalistic terminology, the soul originates in the spiritual world of Atzilut ("Emanation"). That world contains ten sefirot, or levels, and the soul gradually descends throughout these levels until it reaches the last level, Malchut—at which point the baby is born.
The soul's nine-step journey manifests itself physically in a nine-month pregnancy.
Click here if you'd like to gain a basic understanding of the sefirot.
Yours truly,
Rabbi Yisroel Cotlar

Why 9 months? I have given this question a lot of thought over the years. Chazal note that Adam and Chava were evicted from Gan Eden with meda kneged meda (measure for measure) din. Both arguably defied G-d with a level of arrogance at least subconsciously driven by a desire to surpass rather than serve G-d. Avraham created the ideal that we walk ahead of G-d, but deferentially so. Adam and Chava arrogantly tried to walk ahead of G-d. G-d is viewed as 8 because He is ahead of the natural order symbolized by 7 where all that precedes serves that which follows. Thus, Adam and Chava arrogantly behaved like 9 and were accordingly punished with 9 as the reference point, B"H! Notably, the gamatria of Chava's name meaning life is 19 rather than 18 like Chai. Mankind served G-d, Who is 1, like 9 thereby perverting Chai (18)! The 7 days of creation are bookended by Chai as G-d begins with 10 utterances and 8 is implicitly ahead of the 7 days of natural order collectively equaling 18, a remez!