Loneliness comes from the illusion of separation. There are actually two ways to approach it. One is to realize that even when we are with somebody, we are still "alone." It is not really possible to be "with" somebody in the sense of a relationship. That is a delusion created by the ego, a sense of clinging and attachment. This is not to say true love isn't real. Naturally it is real, and the bonds between people in love hold strong indeed. But we remain each entities unto ourselves, with different thoughts, and feelings, and perceptions. So although we may be close in proximity, we can still be as distant as two stars in space.
This brings us to the second way of looking at this delusion of loneliness. Although it appears we are separate entities, this is not really the case. Our minds and perceptions may be unique to us, but we would be fundamentally different without the influence of others acting according to their own thoughts and perceptions. For example, I would not be the person I am now if you were not the person you are now. Knowing you changes me. In fact, every person we meet changes us to some degree or another. Just think of the implications. You make an impact on everyone you meet, for better or for worse. What a tremendous responsibility. And the connection doesn't end with human beings.
We are all interconnected with our environments in complex ways far beyond our understanding. Like the "butterfly effect," nothing in the world, or even the universe, happens independently from everything else. It is not possible to bounce a ball on the street without both ball and street. Likewise, it is not possible to be born into the world without you, the world and everything in it. Everything is connected, and there truly is no such place as "far away."
Proximity is a poor measure of solitude, because it is never really possible to be distant from anybody. It comes back to the old saying, "No matter where you go, there you are." Time and space are relative, especially in regards to people and things that hold special meaning in our lives. Even death isn't much of a boundary when we realize the depth of our connectedness.
It took me thousands of miles and several years to come to this realization. It happened on the beach the first time I reached the West coast. As I let the Pacific Ocean wash over my bare feet,

So I never feel lonely. How could I? Everything in the whole world is right here. Where else would it be? I am here, you are here, and here we are, along with everybody and everything else in the universe. It is not being alone that makes us feel lonely, but the illusion of separation. If we see the truth...that we are already together, every one of us...our loneliness vanishes like a shadow in the sun.