This pulls lessons from all over, but especially from Apocalypse World.
Roleplaying games are a conversation. Like any conversation, they’re at their best when the people in them are engaged and present, not distracted. Playing an RPG means sharing a collaboratively created world and holding that mutual fiction in your mind; thus, the conversation suffers when people disengage.
So how can we keep each other engaged, and avoid having players check out? When I say “you” I’m assuming that you’re the storyteller, but most of these details work for other players too.
First, maintain the shared fiction in ways that are easy for your players to access and interact with. This will vary by individual: some people really need physical props or note cards in front of them, some people like hand-scrawled maps, and some do fine with only words. Ask about what works for your players, and make sure that you’re giving them what they need. Combine multiple approaches when possible, to make a more fully textured experience.
Second, immerse your players in the fiction however you can. This can take many forms: special lighting, background sound or music, making sound effects yourself, or embodying the NPCs you’re portraying. When you act in the fiction, describe your actions in terms of the fiction first and foremost: instead of “the bugbear’s 19 beats your 17, she’s winning the arm-wrestling match,” try “the bugbear breathes deep, sweat breaking out on her brow, and pushes hard against your grip. With her 19, she forces your hand closer to the table.” Just as importantly, address the characters: use the character’s name when you look at a player, and ask the character what they’re doing.
Third, connect directly with players. Just like with other conversations, talking to someone face-to-face is better than only using voice, voice is better than only text, etc. If you’re able, make some eye contact. Remove obstacles between you and other players whenever you can; if you have a GM’s Screen, keep it to the side or get out from behind it every so often—it’s okay to hide your notes, but don’t hide yourself. Your attention, physicality, and presence are tools that you can use to further engage other people, and it’s worth practicing using them as such.
Fourth, remove external distractions. I personally dislike having computers or phones at my games, even when I need them for reference material or playing music. Sometimes those devices are too useful to avoid; when you’re stuck with screens at a table (or as part of an online game), you can make an agreement with the other players to avoid using them for anything besides the game. If people get distracted (it happens), call them back to game and move on. If it happens repeatedly, try to address what—if anything—pulled them out.
Fifth, make space for breaks and socializing! Hanging out with friends is good, and no one can focus perfectly forever. Sometimes the group is punchy and in need of some joking around, in-character or out. Sometimes you need to pause and check how everyone is doing, sometimes you need a breather so that you can eat snacks or figure out what comes next, and sometimes you just need a bathroom break. Make sure that your group is taking time to take care of itself, physically and socially.
Obviously, there are times when you can’t or don’t need to do all of these things. I’ve played good games with friends via instant messenger, for example, with zero personal contact beyond knowing all the other players well beforehand. But that game worked in part because we did such a good job with all the other elements here, and these are all good things to have your gaming toolbox.