Mormons view humans as ultimately spirits, who lived with God as spirits before they were born on earth. God chose a specific time and place for each spirit to come to earth to receive a physical body. God did this so humans can gain essential experience and prove themselves worthy to return to live with God forever.  Thus the purpose of life is to undergo experiences and cultivate virtues that could not be done in heaven as a spirit, and earn the right to return to heaven with God.
As an official Mormon website explains this doctrine:
You didn’t suddenly spring into existence the moment you were born. You were happy in Heavenly Father’s presence, but He knew that you needed more in order to progress. You did not have a physical body like you do now, and you needed a chance to gain experience on your own—away from His presence, but with the ability to communicate with Him and receive help. So He sent you to Earth, hoping that you would return to Him and receive everything He has to offer you. Before you were born, you lived with your Heavenly Father as one of His beloved spirit children. You knew and loved Him, and He knew and loved you. Although you have forgotten your life before you were born, your Heavenly Father has not. He knows you and loves you. He wants you to come to know and love Him, too. 
In contrast to some Christian denominations that emphasize divine predestination and/or the crippling effects of original sin, Mormons teach that humans have complete free will to do good or evil.  Children are regarded as free of sin and incapable of sinning until the age of 8, when they become accountable. 
- "Core Beliefs and Doctrines." LDS.org Quick Facts.
- "Where did I come from? Mormon.org. See also Does God know who I am? and and "Why am I here on Earth?" at Mormon.org.
- "The ability to choose." Mormon.org.
- "Core Beliefs and Doctrines." LDS.org.