The Veil was a clockwork construct within the castle inside the Twelfth Doctor's confession dial, capable of taking a form out of the Doctor's nightmares, it was tasked with interrogating him for information about the Hybrid, killing him over and over as he failed to answer. Its touch was burning and, in the long run, lethal. However, the being had no ill will towards the Doctor, even trying to help him escape the prison.
The Veil pursued the Doctor at exactly the same, slow pace, never relenting. Monitors located throughout the castle showed its viewpoint, letting the Doctor know where it was at any given time. The Doctor deduced that it was designed to force him to confess his deepest secrets. Whenever he did so, the Veil would freeze and the castle would reconfigure itself, bringing him closer to Room 12 and the way out. Having grown fond of the Doctor over the course of his imprisonment, it considered itself his companion.
Arriving in the Confession Dial
After being made warden of the Confession Dial (PROSE: The Veil) by the Time Lords, (TV: Hell Bent) the Veil was briefed about the Doctor. What it heard was mostly "bad things", but it hoped they were unfounded and it would be able to get along with the Doctor, although it knew its role was to force the Doctor to confess what he knew about the Hybrid against its will. The Veil did not know what the Hybrid was or why its masters wanted to learn more about it, and hoped it wouldn't take long to get the answers it needed out of its prisoner. (PROSE: The Veil)
Interrogating the Doctor for the first time
After the Twelfth Doctor was teleported into the Confessional Dial, the Veil followed him around for "a bit" and took the form of his own worst nightmare, (PROSE: The Veil) an old woman who died when the Doctor was "a very little boy"; her body had been covered in white veils, but it was a hot summer day and the corpse attracted flies, a ghoulish sight which gave the Doctor nightmares for years. Chasing the Doctor around the castle, the Veil got the Doctor to confess that he was scared of dying, that he had left Gallifrey because he was scared, and that he knew what the Hybrid was. (TV: Heaven Sent)
Within Room 12, the Doctor found a wall of solid azbantium marked with the word "Home". He realised the ultimate secret his captors wished him to confess: the true identity of the Hybrid prophesied to conquer Gallifrey. Unwilling to give up such valuable knowledge, the Doctor began to punch the wall, allowing the Veil to catch up to him. (TV: Heaven Sent, PROSE: The Veil) Not wishing to let the Doctor injure himself beyond a point where he could answer its questions, the Veil tried to pull him away, and realised that its touch was fatal to Gallifreyans after it fatally injured the Doctor. A dismayed Veil was sent back to its original position in the Castle, (PROSE: The Veil) while the Doctor, in the day and a half it would take him to die, hauled himself back into the teleport room and wired himself into the controls, burning up his current body to bring an identical copy of himself into the castle.
The Doctor left himself a single clue, the word "Bird" written in the sand, to remind himself of a story about the meaning of infinity. This allowed each version of the Doctor to realise what he needed to do. (TV: Heaven Sent) Not yet understanding the Doctor's ploy, the Veil wrote in its log that it would simply have to start over. (PROSE: The Veil)
Loops upon loops
On the second day of his imprisonment, the Veil witnessed the revived, reset Doctor going through much the same things as the day before, though now with the information left to him by his burned past self. The Veil "took offence" when the Doctor began hypothesising that the Castle reset automatically: in truth, the closed energy loop was maintained manually by the Veil itself, who also prepared the Doctor's food.
The loop kept repeating before a helpless Veil who found itself killing the Doctor over and over for no real purpose. By the seventh loop, he had stopped keeping a log of the happenings in the Castle, since they were always the same. The only, inexplicable bit of variety came on the 83rd day of the 235th year, where, to the Doctor and the Veil's confusion, a pigeon had somehow made its way into the Confession Dial and chirped in the rafters (PROSE: The Veil) upon the Doctor's question "are there birds here?". (TV: Heaven Sent) The pigeon was never spotted or heard from again and left the Veil endlessly confused. (PROSE: The Veil)
Trying to help the Doctor
By the twelfth day of the 7000th year of the Doctor's imprisonment, the Veil, driven to near-maddening levels of boredom by the endlessly repeating loop, had decided the best course of action it could take was to try to help the Doctor escape faster, thereby freeing not only him, but also the Veil itself. It realised that the Doctor's scheme could be improved if he took the spade from Room 12 with him to the "last square" and used it to hit the wall, instead of punching it bare-knuckled: unbeknownst to the Doctor, the spade, being made of an amalgam of duralinium and dwarf star alloy, was strong enough to chip azbantium.
On the next loop, the Veil thus put the spade in a conspicuous position and left markings prompting the Doctor to use the spade. However, the Doctor spotted the Veil watching him, and, growing suspicious, chose to ignore the spade when he left the garden, (PROSE: The Veil) as he had done before. (TV: Heaven Sent)
The Veil made more and more blatant attempts to get the Doctor to use the shovel on every loop until the seventh day of Year 200,435, when it finally decided it was no use to try and change the Doctor's mind and gave up. (PROSE: The Veil) Things continued as before (TV: Heaven Sent) for over four billion years' worth of loops. (TV: Hell Bent)
On the 42nd day of Year 4,500,000,003 of the Doctor's imprisonment, the Veil noticed that the Doctor was just one punch away from breaking through the wall and escaping. It wrote in its log that, having grown quite fond of the Doctor by now, it hoped that the Doctor would take it with him when he escaped, even fancying itself the Doctor's companion. (PROSE: The Veil)
When, on the next loop, the Doctor finally got through the wall and stepped out into Gallifrey, the Veil tried to step behind him, but as soon as Gallifrey's sunlight hit it, it crumpled to the floor into a pile of disconnected cogs and gears. (TV: Heaven Sent)
Behind the scenes
During the Doctor Who: Lockdown! watch-along of Heaven Sent in 2020, director Rachel Talalay revealed that Steven Moffat's original script compared the Veil's appearance to that of a beekeeper, and also released an early concept of the Veil's appearance as well as stills from many cut shots of the Veil's point of view on various other parts of the Doctor's prison-castle than could be squeezed into the runtime of the finished episode.
The Veil was never named on screen, only in the credits of Heaven Sent, where it was credited simply as "Veil". In the Doctor Who Extra clip Behind the Veil, actor Peter Capaldi and director Rachel Talalay both refer to the creature as "the Veil". The creature's bio on the BBC Doctor Who website consistently refers to it as "the Veil", and the name was eventually used in a narrative source in the short story of the same name in DWM 521.