How was Harry Potter able to get the Sword back after giving it to Griphook in exchange for him getting Harry into the Vault to get Hufflepuff's Cup?
Both books and film repeatedly assert that the sword is enchanted to present itself to any worthy Gryffindor in need of help. Further, as is explained to our trio when receiving Dumbledor’s bequests, just because the sword presented itself to someone does not make it that witch or wizard’s property.
‘Unfortunately,’ said Scrimgeour, ‘that sword was not Dumbledore’s to give away. The sword of Godric Gryffindor is an important historical artefact, and as such, belongs –’
‘It belongs to Harry!’ said Hermione hotly. ‘It chose him, he was the one who found it, it came to him out of the Sorting Hat –’
‘According to reliable historical sources, the sword may present itself to any worthy Gryffindor,’ said Scrimgeour. ‘That does not make it the exclusive property of Mr Potter.’
Deathly Hallows - page 109 - Bloomsbury - chapter seven - The Will of Albus Dumbledore
It follows that that witch or wizard cannot give it away. Even to a Goblin.
Neville pulled the sword out from inside the Sorting Hat.
After Griphook took the Sword of Gryffindor from Harry, it was with Griphook until Neville pulled it out of the Sorting Hat when the Dark Lord had put the hat on him.
“In one swift, fluid motion Neville broke free of the Body-Bind Curse upon him; the flaming Hat fell off him and he drew from its depths something silver, with a glittering, rubied handle –”
- Harry Potter and the Deathly Hallows, Chapter 36 (The Flaw in the Plan)
Neville was able to get the sword the same way Harry did - by being a worthy Gryffindor. Rufus Scrimgeour, the Minister of Magic, mentioned that it’s reliably known that the Sword can present itself to any worthy Gryffindor through the Sorting Hat.
“It chose him, he was the one who found it, it came to him out of the Sorting Hat –’
‘According to reliable historical sources, the sword may present itself to any worthy Gryffindor,’ said Scrimgeour.”
- Harry Potter and the Deathly Hallows, Chapter 7 (The Will of Albus Dumbledore)
Griphook taking the sword didn’t unlink it from the Sorting Hat, so a worthy Gryffindor could still retrieve it from wherever it was (which happened to be with Griphook) using the Sorting Hat. J.K. Rowling confirmed this in an interview where she’s asked how Neville got the Sword of Gryffindor.
Su: How did neville get the gryfindor sword, is there a link to the hat
J.K. Rowling: Yes, there is very definitely a link to the hat!
J.K. Rowling: Neville, most worthy Gryffindor, asked for help just as Harry did in the Chamber of secrets, and Gryffindor's sword was transported into Gryffindor's old hat -
J.K. Rowling: - the Sorting Hat was Gryffindor's initially, as you know.
J.K. Rowling: Griphook was wrong - Gryffindor did not 'steal' the sword, not unless you are a goblin fanatic and believe that all goblin-made objects really belong to the maker.
- J.K. Rowling and the Live Chat, Bloomsbury.com (July 30, 2007)
There’s a link between the sword and the hat, and that’s how Neville got the sword.