It is generally explained on the basis of karma. When man lives a life of purity and faith in god, he gains moksha and never takes birth again. But when he commits sins and also does some good deeds, he goes to heaven and stays there as long as the effects of his good deeds last. When he runs out of it, he has to take birth again. Hence god created humans to do good deeds and live a life of sanctity.