There is an apocryphal gospel called "The Gospel According to the Hebrews" most of which is lost; in one of the fragments which remain there is an account of this incident which sheds a little light on its meaning. Here is how that ancient text records this story:
The rich man said to Jesus, "Master, what good thing must I do really to live?" Jesus said to him, "Man, obey the law and the prophets." He said, "I have done so." Jesus said to him, "Go, sell all that you possess, distribute it among the poor, and come, follow me!" The rich man began to scratch his head because he did not like this command. The Lord said to him, Why do you say that you have obeyed the law and the prophets? For it is written in the law, "You must love your neighbor as yourself," and look you there are many brothers of yours, sons of Abraham, who are dying of hunger, and your house is full of many good things, and not one single thing goes out of it to them." And he turned and said to Simon, his Disciple, who was sitting beside him, "Simon, son of Jonas, it is easier for a camel to go through the eye of a needle than for a rich man to enter the kingdom of heaven."
There we have the secret and the tragedy of the rich young ruler. He was living utterly selfishly. He was rich, and yet he gave nothing away. His real God was comfort, and what he really worshiped were his own possessions and his wealth. That is why Jesus told him to give it all away.