There are many factors, however we believe that God tests the souls before they are born. The souls that show Iman end up being people with Iman.
This goes back to the topic of predestination or freewill. As Shias we believe that we were neither predestined nor do we have total free will.
There are some narrations referred to as حديث الطينة , the narrations of the soil which indicate that because God knew what kind of decisions we would make in the future (since Gods knowledge is not limited by time), He placed us in the category that we belong. That means if God knew I would be a Muslim he made me a Muslim. If he knew someone who reject it then He made this person far from it.
Before the creation took place the souls existed and each soul showed its own potential, some souls were pure others were not.
I don’t know if an English source on this, there are Arabic sources that discuss it under the topic of حديث الطينة