Because he knew how to compartmentalise
Occlumency is about partitioning up and blocking off parts of your brain, something Snape knew all about. Even before he started working against Voldemort, he’d spent years putting his emotions in boxes. His childhood wasn’t happy, with all the information he gave Lily about Hogwarts suggesting he was desperate to leave his home life behind. And he spent a long time coveting the Defence Against the Dark Arts job, despite being a seemingly committed Potions teacher.
Then, of course, there were his unreciprocated feelings for Lily. We don’t know if she ever suspected, but it seems likely he never told anyone else. Certainly, he asked Dumbledore to keep his love for Lily a secret. Dumbledore, of course, gave his word that he would never reveal what he saw as the best part of Snape – and after that, Snape locked his love for Lily away from everyone else, and almost literally took it with him to the grave.
Because he had a lot of practice
His extreme remorse over Lily’s death gave Snape the motivation to effectively fool Voldemort for many, many years. Dumbledore himself said he would trust the dangerous job of spying on Voldemort – giving him just enough information not to become suspicious – to nobody but Snape.
When you break down what Snape did over those years, it was pretty impressive. Keeping Harry safe, whilst also apparently feeding information to Voldemort that put him directly in danger. Making an Unbreakable Vow with Narcissa Malfoy to prevent Draco killing Dumbledore, when he knew Dumbledore was dying anyway. Letting Death Eaters take over Hogwarts, while leading Harry to Gryffindor’s sword. Pretty much everything he did was two-sided. Even in the alternate, Voldemort-flavoured reality we witnessed in Cursed Child, Snape was working undercover. He really was that committed to the double-agent life and, as he himself told Harry, being good at Occlumency was an essential part of the job.
Because he was inscrutable
Harry was never good at Occlumency because he was too hot-headed. He was used to thinking on his feet, used to displaying his emotions, used to acting first and reflecting later. Snape, on the other hand, was cold, calculating, and patient. After all, he waited 17 years to avenge Lily’s death, following Dumbledore’s plan with careful precision, always keeping his feelings firmly to himself. Such patience helped him perfect the art of Occlumency. He rarely rushed into things in the way that Harry did, which meant he could (usually) calmly organise his thoughts when the situation required it. Even when Harry blurted out his concern for Sirius in front of a bemused Professor Umbridge, Snape’s response gave nothing away.
And yet – he couldn’t shut out every emotion
If Occlumency was about shutting down essential parts of yourself, even ignoring your own instincts in certain circumstances, then, for all the reasons listed above, it is no great surprise that Snape was so good at it. He lived a double-life for a long, long time. But there was one emotion he could never keep entirely hidden: his bitter jealously and deep resentment of Harry’s father.
When Dumbledore decided Harry needed to learn Occlumency, he instructed Snape to teach him because he felt doing it himself would expose Harry to more harm. This was an uncharacteristically terrible decision. Dumbledore knew Occlumency was about emotional self-control, and he also knew both Harry and Snape were prone to losing their emotional self-control whenever they were in contact with each other. Snape might have thought he’d protected his particularly negative memories about James Potter by using a Pensieve, but Harry’s impulsive curiosity – so at odds with Snape’s calculated and methodical manner – meant that was a disaster waiting to happen.
When he realised Harry had seen what his father had once done to him, Snape reacted with actual violence. He pushed Harry to the floor, threw glass jars at his head and chased him out of the room. Just the memory of James Potter had enough power to make him forget himself. All his inscrutability deserted him. Which is proof, if proof were needed, that nobody’s mind is completely invulnerable. Not even Snape’s.
In this image:
Because he was motivated
But for the most part, Snape was able to shut down his mind effectively. When he was not being goaded by the memory of James or the reality of his reckless son, Snape’s abilities as a double-agent were unmatched. And it all comes back to that central reason. The essential part of himself that he boxed up and locked away. The best part of himself, according to Dumbledore. His love for Lily. His “always.”