I've seen this question on Quora and decided to add my perspective on this.
I thought I'll share it here as well. Here it is:
People are lonely when they experience a feeling of lack, emptiness inside of them. We then tend to look for something outside of ourselves, that will fill that void. It is largely dictated by our culture/society/education which shapes our behaviors and these sink deep into our subconscious minds.
A lot of marriages are falling apart just because we have certain expectations towards the other person, and we come into relationships "demanding" those to be fulfilled. That's when we are not whole.
When you are whole, you feel complete, you can be happy even when alone, because your contentment comes from inside of you. Your self worth is not dependent upon any external stimuli.
I think this has to do with growing up all "parts" of yourself. We often fail to recognize that we are multifaceted beings. We are not just one person because some parts of ourselves are at the different stage in growing up emotionally.
People that have lack of self worth would be most likely lacking encouragement from their parents as well as experience lack of emotional closeness with them. That's when they grow in years but are still dependent upon approval of some sort of authority and lack self worth, self reliance etc.
It doesn't have to be exact for every case as we are dynamic beings and there are many variables that come into place, but we can generalize like this with high possibility.
This is why I think being lonely means to experience some emotional lack inside of you, not knowing and/or accepting who you are and being dependent on something outside of you for feeling fulfillment, self worth, acceptance etc. (if it comes from outside of yourself, it will always be only temporary)
In this situation you can't be happy when alone and you feel lonely.
It is not the other person that makes you feel happy. You can be surrounded by people and still be lonely.