Meaning is direction. No direction leads to anomie, which brings confusion and a dreadful sense of loss after one thinks on death. People need to feel the pull of a direction to keep motivated. The logic is that if our actions mean nothing in the end, why go on through times of tremendous hardship? Why not just end it? It's a terrifying notion because we all have deeply rooted feelings of self-preservation. If it will have "all been worth it," because our lives became worth more than the sum of their parts and so have been preserved past death. The ultimate idea would be that we are all immortal.
This is why religion is so pervasive. People fear that the religion might be true and so because of this find themselves at the will of whoever created the religion. This is also how many destructive cults are formed (which is what most religions are to begin with). Some people will even go so far as to kill themselves if they believe it will preserve themselves.
To illustrate: If you are cutting steaks and you cut your finger off by accident and it could not be reattached, every time you look at where your finger once was you will feel ashamed and regret it. What was the point of the sacrifice? It was for nothing. If you saved someone's life who was nearly cleaved in two by a giant meteor consisting of jagged metalic rocks, but all you lost was your finger and you became an instant celebrity for it, then millions of dollars poured in from people from around the country in sympathy, you will probably look upon your knub later with a sense of pride and a sense of wonder at the awesome phenomenon. You achieved something worthwhile (because you believe the value of your finger was much lower then the end result, which was the life of another saved and heaps of useful money and glory). The same holds true with life. If you can see your hardships and then finally the loss of your life as paling in comparison to the awesomeness of your purpose, then you won't be so terrified to die. It will all have been worth it.
I am unsure how much of this phenomenon is taught and how much manifests itself innately.
However, I don't think it's necessary to trick yourself into feeling comforted by such things as cults, spirituality, quack nonsense, or religion. I think a person can develop meaning for their lives and to exist onward in the lives in others, even after death. One must realize that they will leave their mark upon the constantly changing surface that is the world of the living. In this sense, one lives on past their death and it will have all been "worth it," if they did something they felt was worthwhile, such as help others. To get through, day to day, a person needs to be motivated to at least get up and live in order to be happy. Ambition, motivation, and capability then determine whether a person feels happy. The higher the ambition, the more need for motivation and capability in order to feel as though, "it will all be worth it," to get through and be happy for doing so. Some cling to material goods, some cling to "riches in heaven," still others cling to their pride, some by bolstering it through helping others, which is a commonly repeated theme of how one becomes a "better person." (Not to say that every seeming selfless action is actually selfish. I think people being happy simply because others are happy and sad simply because others are sad is an indication that our motivations are not always purely selfish. I think humans want others to be happy and the resulting happiness that one feels around others who are happy isn't the motivation for that. It is something inate and unadulterated by thought and motivation, it is a reaction.)