Everyone needs a shoulder to cry on, literally or figuratively. If you're lucky, sometime in your life you will be the person someone goes to, whether they are a friend, family member, loved one or even a complete stranger. When that time comes, there are some things I like to keep in mind. I've learned these things from other people's experiences, my experiences as a listener and my experience as the one being listened to. All of this is my personal opinion, so please let me know if you have another viewpoint I haven't thought of.
When you're listening to someone, make sure listening is your first priority. In daily conversation, most of us spend much of the time we are not speaking thinking of the next thing we are going to say, rather than listening to the thoughts and ideas of the current speakers. When someone's pouring out their frustrations to you, they are the most important person in the conversation. They won't care what you have to say about their troubles if it's clear you aren't absorbing what they are saying.
When someone needs someone to talk to, they are usually looking for just that—not someone to tell them how to fix their problems. Telling someone how to fix their problems after listening to only a brief overview of their situation comes across as patronizing. I guarantee that whatever solution you think of, they likely already have too, but, for whatever reason, it isn't feasible for them in their current state.
Putting your problems into words is cathartic and builds confidence. It's that relief that the person is looking for, and it's that confidence that might give them the ability to face their problems. If they explicitly ask for help or you honestly believe you know something they don't, be kind, understanding and move forward at a pace comfortable for them. Something that is easy for you might be difficult for someone else.
Everyone needs validation, especially someone in need. When someone comes to you for help, they need you to tell them that what they are feeling and thinking is okay and that they are not alone. When you're sad, it's easy to feel ashamed or upset with yourself for being sad, which only makes things worse. A gentle reminder from a friend that emotions, even bad ones, are normal, and it's okay to feel them for a while. It's okay to not be okay!
You can still be a good friend and a good listener without understanding what someone is going through. For example, if someone is struggling with a mental illness that you personally aren't struggling with, tell them, "I don't know what it's like to have anxiety/depression/etc., but I'm always here for you to talk to." The last thing someone in need wants is for the one they go to to smile and nod and pretend to understand. Honesty shows compassion and respect. Admit the gaps in your experience and knowledge, and you can be a friend to anyone.
This has all been incredibly vague, but that's because I don't want to exclude anyone's situation. A good listener can make someone feel better whether they performed poorly on a test or are in the depths of a depressive episode. You might not be able to fix someone's problems, but the fact that simply listening can provide a bit of relief is a wonderful thing.