Poké balls can be rather disorienting for some pokémon. They’re aware of the outside world, but they perceive it in the same way you might perceive a dream. You may not be aware of your body exactly, and you might feel as if you can’t interact with what you see—not really, anyway. For some pokémon, this can induce anxiety and trigger their flight response. As a note, many other pokémon—more than the few who dislike poké balls—feel more safe and comfortable inside their balls than outside; it’s more a personal preference to stay inside.
(Also as a note, no, I do not know this from personal experience, contrary to popular belief.)
That having been said, a pokémon needs to want to stay inside. If their need to escape is so powerful that it overwhelms their poké ball’s closing mechanism, then the ball will fail and subsequently release them. However, if a pokémon merely wants to escape but not with everything they’ve got (or perhaps their want to stay inside outweighs their need to escape), then their balls will remain closed.