We're saved by repenting of our sins, accepting what Jesus did on the cross to pay for those sins, and by accepting Jesus as our Lord. If Jesus is our Lord we will do whatever the Bible commands.
We're not saved by being good. You can't have a pure heart without being saved and once saved you can't lose your salvation. As the Bible says,
(Ephesians 4:30 NIV) And do not grieve the Holy Spirit of God, with whom you were sealed for the day of redemption.