Many times a friend of mine posts something about how they're feeling awful about something and I really want to comfort them but I never seem to know how. I'm worried that maybe some things I say might make them feel worse. How do I prevent that? They need to know that I'm their friend who can be there for them when they need me, but I never have any idea how to talk to them.
The problem with people is that people are different, and what works for one person may not work for the next, and sometimes what worked before for someone won't work again the next time for the same person. Context, background, all that matters. But sincerity and openness can go a long way. You could tell them what you just wrote: I'm your friend, I'm here if you need me. Let them know you're not really sure what the right thing is to do or say. They may have something specific in mind that you can do to help -- like distracting them with something fun, or not talking about the problem, or letting them vent without trying to solve their problems -- or they may not know, and will likely just appreciate the thought. Sometimes there isn't something you can do or say to comfort someone and all that can help is time. But speaking as someone who has spiraled into deep depression from which nothing could fix but medication, knowing that my friends still cared about me was enough.
Frustratingly, different humans will have different needs in this area, and even will frequently want different things at different times So, I honestly just ask. "Hey, do you want advice, do you just want to vent, or do you want me to distract you?"
You can also ask when they're not upset and find out what generally helps them. You might get more useful answers since some people have trouble knowing or communicating what they need when they're upset. Combine with communicating and asking when they're upset and mix in a little trial and error and you have a recipe for finding out what helps each individual.