God gives human beings a spirit that, not conscious of itself, works in concert with the human brain to form the human mind, giving us the ability to understand language, writing, math, and the like. Paul explains: "For what man knows the things of a man except [by] the spirit of the man which is in him? Even so no one knows the things of God except [by] the Spirit of God" (1 Corinthians 2:11).
To answer the question, we can turn to the book of Ecclesiastes. Solomon wrote that when we die, "the spirit will return to God who gave it" (Ecclesiastes 12:7). So at death, one's spirit returns to God, while the body on earth decays or is destroyed. God keeps our spirit with Him until the time we are resurrected. He will then place our spirit in our refashioned risen body, the spirit apparently acting as a permanent record of what we are—including our memories and personality—so that our individuality is retained.
One example of people being restored to life with the same individual identities is the second resurrection, which is a resurrection to physical life for all who never had a chance to hear or understand God's truth. God through the prophet Ezekiel tells of the resurrection of one group of people, Israel: "Surely I will cause breath to enter into you, and you shall live. I will put sinews on you and bring flesh upon you, cover you with skin and put breath in you; and you shall live" (Ezekiel 37:5-6).
When these people are resurrected to physical life, their memories will return to them, and they will think they are cut off from God: "Son of man, these bones are the whole house of Israel. They indeed say, 'Our bones are dry, our hope is lost, and we ourselves are cut off'" (Ezekiel 37:11). If their memories didn't return to them, through the spirit in man, they would know nothing!
Our publication, Heaven and Hell: What Does the Bible Really Teach? includes a section titled "The Spirit in Man". It is helpful to understanding this subject.