There are many theories about how to maintain immersion in games, and no single ingredient that does it all. But here's one framework I've found useful personally:
It's a psychological theory of motivation called Self Determination Theory. It says that humans (players) are motivated by three core drives: Autonomy, Competence, and Relatedness.
Autonomy is an alignment between what you're doing and what you personally value. Few things will sap a player's investment in a story faster than a long sequence of actions they don't care about but have to do/read through anyway.
In games, autonomy is often mistaken for "choice," but giving tons of wide-open choices is basically throwing eggs at the wall, hoping one of them lands on what the player cares about. Instead it's more effective to identify a few core values the player is likely to be pursuing, and drive key moments around threats or opportunities related to those values. Even stronger is when you can put those values in conflict, so the player needs to reflect and make very personal choices about which to prioritize. This promotes a "volitional" mode of play, where players are drawn into actively striving toward goals they care about, even when real choices are sparse.
It may seem like a silly example, but play deep enough into Hatoful Boyfriend and you'll find that some of the most deeply immersive parts of that visual novel are almost entirely linear. They work by building the player's investment early in the game, and establishing high stakes targeting those values, so the player is motivated to push through the available path.
Giving players in-game tools to express what they value and why can help cement this feeling of autonomy, even when the player has to take actions contrary to those values. Just including a reluctant yes option when "no" isn't available, or conversations where the player can express their conflict, can let the player feel listened to, and allows them to act in a way that's consistent with what they care about even when they have to make compromises or bump into the boundaries of what the written possibility space allows.
Competence is a feeling of control and capability, that your skills are up to the tasks you're pursuing, and that you are growing and learning over time. Traditionally this is an area where games have excelled through their tight loops of challenge, action, and reward, driving the player to learn, apply, and master their mechanics, and progression systems that show you measurable growth even during learning lulls.
It can be more difficult to create a sense of competence in a visual novel game though. Because the options available to the player at each juncture might be totally unique, they can have a hard time predicting the consequences of each option and making deliberate, meaningful choices. Every time you hit "oh, wait, that's not what I expected that choice to do!" you've put the player in a position of not feeling competent.
Ensuring your story and world have what Emily Short calls systematic mechanics can help with this: rules and conventions your player can learn over time and apply to inform new choices in scenes they've never seen before. The developers at Inkle for example conceived of 80 Days as a board game at its foundation, where the player can exercise their competence, with the story layered on top providing context, nuance, and richness. This lets the player choose with purpose, and develop a mental model of how to make progress toward their goals.
Relatedness is a feeling of connection with other people, of belonging in a community, or being a part of something bigger. It's strongest when used with other real players, but it's possible to cultivate strong relatedness with non-player characters too. Most normal writing advice applies here, about creating characters with depth and motivations, so they don't appear one-dimensional.
In games we also have the ability to shape interactions and dependencies on the characters - giving the player the opportunity to rely on another character for support, or make sacrifices to help them, can help foster strong relationships. One reason Aeris's death in Final Fantasy 7 was felt so keenly by so many players was that she served as the player's primary healer, so the narrative loss of the character was backed-up with an "oh shit, what now?" sense of dread from their practical gaming instincts too.
Once you're structuring your game around these core drives, you can also subvert the theory and selectively deny the player one of these axes to create emotional turning points.
For more on applying self-determination theory in games, I recommend checking out the work of Scott Rigby and Troy Skinner of Immersyve. They've developed a model based on SDT they call PENS (Player Experience of Need Satisfaction) and they've given many presentations exploring how these aspects drive play. (Granted, they also want to sell their services, so take this with the requisite grain of salt too)