But when someone else is dealing with it I panic and have no idea what to do and am terrified of misstepping.
The best thing you can do IMO is listen. Just, let them talk. If they don't want to talk about the feelings, you can also just sit with them - silence is OK, and also, they are likely to start talking. Validate and empathize - it can feel like doing nothing to say, "I understand what you're saying" - or to say "you are in so much pain, and that is such a difficult thing to be with".
Things to remember for yourself - which I know you know, just am offering as a reminder - you don't have to talk them into or out of anything. You just need to tell them you love them, and that you are there, and you're able to listen to their pain. When possible, avoid "I'm sorry (you're hurting so much, or you're feeling this, etc." - you haven't done anything to apologize for, and they may inadvertently feel badly for causing you to feel badly, which can then shut them down.
On a basic level, what you're doing is normalizing their pain, which you can do simply by listening - because suicide is something that people tend to feel they cannot talk about without there being a negative consequence. When they can talk about it, and they realize that they aren't hurting YOU, that it's OK and you can handle it - they have a chance to release a lot of that built up inner pain, grief, sadness, hopelessness - and if they can release enough of it, they will actually feel somewhat better, even if only for a short time.
Try and make sure you stay in touch with where you're at, too...I know there are plenty of times (for myself) that I honestly can't handle hearing someone else talk about suicide, it's too raw for me and I'll end up worse off. I think one of the hardest things is to have someone reach out, and not be in a head-space where you can help. It's never a bad idea if someone is struggling, to find out who else they've shared these thoughts with. You may need to call on them, to also help the friend and let you have the space you need.