"I don't care what happens to these people."
These are the Eight Deadly Words, a phrase coined by Dorothy Jones Heydt back in the early 1990s for the moment the reader or viewer checks out of a story. Once the reader stops caring about the characters of a story, abandoning it for a better one isn't far behind.
So, how do you avoid the Eight Deadly Words?
Heydt was originally talking about characters being bland, unlikable, unsympathetic, or unengaging (or all of the above) to the point of generating apathy, but that's not the only reason a reader could stop caring. This means that there are a number of things to watch out for as you are writing your narrative:
Understand why the reader is most likely invested (or going to be invested) in your story. What people are looking for in a story can differ based on genre and story focus. If you are writing a romance but the romance never appears to go anywhere, eventually the reader will get bored and stop caring. Likewise, a horror story without scares will eventually cause a reader to walk away. The same goes for resets to a status quo - if one resets to the wrong status quo, one risks undoing a key element that kept the reader engaged, causing the reader to lose interest.
Give your characters plenty of character moments, particularly small ones. While the struggles of a protagonist against an equal or greater force of antagonism fuels the conflict of a story, it does not define who a character is - that is done by the character moments. While big character moments (such as reacting to a major setback, showing who the character really is) are necessary, these are by necessity few and far between. It is the little character moments that flesh out the character and make them "pop" - and by extension, make the reader care more about them, and these should happen fairly frequently.
Ensure that your characters are empathetic. The reader does not need to like or sympathize with your characters to care what happens to them (in fact, hating a character and wanting to see them get their just desserts IS caring about what happens to them). But, the reader does need to be able to understand where they are coming from. Their actions have to follow naturally from their circumstances and goals. If characters appear to act in a random fashion or are appearing to be doing things only because the story told them to (this is sometimes called "character derailment"), they feel artificial and unreal to the reader, and cease to be empathetic.
Ensure that the characters have a reason to hope. This is not to say that you can't write a tragedy, or a story in which everybody dies at the end. But, within that story, there must be hope spots for the characters - they should not be facing a never-ending conga line of trauma and loss. If there are no hope spots, there is a very real risk of the reader checking out just to avoid the unending misery.
Maintain suspension of disbelief. In a very real way, suspension of disbelief is a key mechanism for keeping a reader involved in the story. If it is broken, the shock of the break will pull the reader out of the story, and this can often include ceasing to care about what happens to the characters inside it. This does not mean that one cannot write stories that are outlandish, or crazy - fantasy is a genre that is filled to the brim with outlandish and crazy things, and these don't stop readers from maintaining their suspension of disbelief - but it does mean that one should avoid writing things that violate the established internal rules of the story, or come across as stupid or unbelievable within the confines of the story.
A really good example of this done right is a show by John Logan called Penny Dreadful. While it is a horror mashup with some truly bizarre elements, it avoids every single one of these traps. Almost every scene gives the characters small character moments, every character is empathetic (even when they do immoral or horrifying things), the scares and horror are present where they need to be, and once the internal rules of the story are set, they are never violated. The end result is a compelling story where one cares about what happens to every single character.