Also, the rich young ruler was breaking a commandment—though not by outward sin like stealing or lying, but in his heart. God’s first commandment is: “You shall have no other gods before Me” (Exodus 20:3), and Jesus saw that the man’s attachment to wealth had become a god to him. By valuing his possessions more than God, he was disobeying the commandment to love God with all his heart (Matthew 22:37). Jesus emphasizes again and again that sin begins in the heart; obedience is shown by what we truly love and trust. The story shows that placing anything above God—even money—is breaking His law in the heart, which is exactly what Jesus calls us to correct through love and obedience.
I think it is especially peculiar is Mark 10:17's observation of Jesus' regard toward the rich young ruler, "Jesus looked at him, loved him, and said to him..." Mark thought Jesus' love as noteworthy while Matthew and Luke might have left it as going without saying and I agree that this tidbit shouldn't be overlooked or taken for granted as I'm certain the rich young ruler completely missed the sentiment, as evidence of his asking "what must I do (that is, to secure God's love and earn) to enter...?
We love God because (we come to accept that) He first loved us, and we love God with all our heart only when we've realized the full depth breadth and width of that love extended toward us. Jesus was aware of where the young ruler had directed his affections and effectively demonstrated the void in love that all his possessions 'returned' to him. I mean, what is sadder than an unrequited love, so it's no wonder that he went away sad. However, if he'd just accepted that 'trivial' fact that Jesus loved him... he would've found heaven within himself.