The Angel in charge of conception is called לילה / Leila. When Hashem wishes a human being to be born, He bids the Angel Leila "Bring me this neshama from Gan Eden". The neshama, though, resents being uprooted from its Divine source, and complains to the Almighty "I am pure and holy, linked to Your Glory. Why should I be degraded by having to enter a human body?" Hashem responds: "The world where you will live surpasses in beauty the one from whence you emanated. You were fashioned for the sole purpose of becoming part of a human being and being elevated by his deeds."
The meaning of this is that although in Olam Haba the soul enjoys undisturbed tranquility and bliss, nevertheless the present world, despite all its tribulations, is of greater beauty. Only as long as a person lives on earth does he have the opportunity to study Torah and fulfill the mitzvos, thus accumulating merits.
Hashem subsequently compels the soul to merge with the seed for which it was destined. Even before the fetus is formed, the angel inquires of Hashem "What shall be its fate?"
At that point, the entire future of the unborn child is preordained. Hashem determines whether it is to be male or female, whether he or she shall be healthy or suffer from some sickness or handicap, his appearance, the degree of his intelligence, as well as all his mental and physical capabilities. Moreover, all particulars of his circumstances are already decided - whether wealthy or poor, what shall he possess, and who will be his future spouse.
We see that all details of a person's life are predestined. However, there is one exception. Hashem does not decree whether someone will become a tzaddik or a rasha. Each one decides how to fashion himself by means of the faculties and capabilities that were pre-ordained for him.
A person should not feel pride in his intelligence, strength or money, for these qualities are not of his own achievement, rather they were Divinely decreed for him before birth. There is only one field of endeavour in which accomplishment results from the individual's effort - whether and to what extent he will study Hashem's greatness by delving into His Torah and emulating His ways. To the degree in which he succeeds in this endeavour, he has actually accomplished something for himself.
While still in the mother's womb, the child is taught the entire Torah. He is shown a vision of both Gan Eden and Gehinnom, and the angel in charge of him entreats him "Become a tzaddik! Do not become a rasha!" When the child enters the world, the angel strikes his lips, causing all the Torah knowledge previously imparted to him to be forgotten. [Nevertheless, that knowledge was absorbed by his subconscious mind, enabling him to retrieve it during his lifetime].
Source: The Midrash Says