As others have mentioned, it's typically something that did okay-not-great (or sometimes even outright poorly) in theaters but over time manages to grow a fanbase that tends to be a bit more dedicated and (for lack of a better word) evangelical about it because for them it feels like it was unfairly overlooked or just better than some other movies that do become major successes.
That's the rundown, but as to the mechanics of why some movies fall into this and others don't. I think that has to do with what a movie has to say and how it says it. Big, accessible, mainstream movies that are built with everyone in mind are also often laden with cliches and reliable tropes that we're very familiar with. This is a tool and done on purpose for the sake of making it easier to short-hand character and story elements and push through the movie expediently without the risk of an audience member going "hey wait, back up, what?". Movies that tend to become cult classics are the ones that, for whatever reasons, try to do things differently. Sometimes in big ways, sometimes in very small ways. They're the ones that might actually have a specific morality or philosophy on display, with characters that might indulge in monologues or (effectively) lectures about some topic or another. In other words, they have content to them that goes beyond the piecemeal tropes and cliches we're accustomed to. This creates the effect of both sometimes making them more difficult to watch and 'get' everything in a single viewing but also making them more rewarding when you do put in that extra legwork towards figuring it out. As well, sometimes it is simply a topic/content that you agree with and that alone can make a person a fan.
Aside from all of that, sometimes it's simply just a question of structurally making something that is more distinctive and unique.