Occlumency is the art of magically defending the mind against external penetration, sealing it against magical intrusion and influence – the defensive counter to Legilimency. A practitioner of Occlumency is referred to as an Occlumens. It is a specialized branch of magic, not usually taught at Hogwarts.

Occlumency is a necessary prerequisite to defeat a Legilimens' lie-detector abilities without suspicious behaviour such as avoiding face-to-face contact and eye contact. Elementary Occlumency involves clearing the mind of thought and emotion, so that the Legilimens can find no emotional ties to memories that the target wishes to conceal. Simple resistance to attack requires similar skills to those needed to resist the Imperius Curse. In its more advanced form, Occlumency allows the user to suppress only feelings and memories that contradict what the user wishes a Legilimens to believe, thus allowing the Occlumens to lie without self-betrayal (OP24).

Occlumency has not often been studied at Hogwarts in the years prior to the second war against Voldemort, particularly not below N.E.W.T. level.

Severus Snape is a particularly skilled Occlumens, since he has managed to survive as a spy among the Death Eaters, under the nose of Voldemort, a noted skilled Legilimens who has good cause to be suspicious of Snape's loyalty (DH33). Dumbledore knows enough Occlumency that he can teach it if he wishes to (OP37). Harry Potter learned some of the basic theory of the art, but ordinarily had a poor success rate in defending himself against attack. Dumbledore pointed out to Harry that he had never been a good Occlumens (HBP25).

Readers noticed that Draco was able to perform Occlumency after being taught by his Aunt Bellatrix while Harry could never perfect that skill after half a year of lessons with Snape (HBP15). Many agreed with Harry that Snape was somehow sabotaging him, or making it easier for the Dark Lord to penetrate his mind, but that was not the case. Harry was just unable to become an Occlumens. JKR explained the different abilities of Harry and Draco this way:

I think Draco would be very gifted in Occlumency, unlike Harry. Harry’s problem with it was always that his emotions were too near the surface and that he is in some ways too damaged. But he's also very in touch with his feelings about what's happened to him. He's not repressed, he's quite honest about facing them, and he couldn't suppress them, he couldn't suppress these memories. But I thought of Draco as someone who is very capable of compartmentalizing his life and his emotions, and always has done. (TLC)

Thanks to the Horcrux connection between Harry and Voldemort, they could sometimes see into the other's mind, which is how Harry saw the murder of Frank Bryce in the Riddle House (GF1), while Voldemort sent Harry the vision of Sirius Black being tortured in the Department of Mysteries (OP31, 0-32). Harry began Occlumency lessons to close his mind when Dumbledore realized he saw the attack on Arthur Weasley at the Department of Mysteries through the eyes of the snake Nagini, who was also a Horcrux (OP21). Voldemort tried to possess Harry at the Battle of the Deparment of Mysteries, but realized it was too painful for his wounded soul to cohabit with Harry's whole soul. So for a year after that the Dark Lord used Occlumency to keep Harry out of his mind and his business. But Harry could not be blocked all the time, and as each Horcrux was destroyed, he began to see almost everything from Voldemort's point of view, even the memory of the Halloween night in Godric's Hollow when James and Lily Potter were killed (DH17). During the Battle of Hogwarts, Harry realized he could use his "window" into the Dark Lord's mind to "remotely view" his location and strategize against him (DH32).

That the scar-connection to Voldemort grew stronger as the Horcruxes were being destroyed was no coincidence according to JKR:

Question: How come Voldemort was no longer employing Occlumency against Harry, as he was in the 6th book J.K. Rowling: He is losing control, and unable to prevent Harry seeing into his mind. The connection between them is never fully understood by Voldemort, who does not know that Harry is a Horcrux. (BLC)