Sword of Gryffindor
The sword can appear to any worthy member of House Gryffindor.
In JK Rowlings own words:
Gryffindor's sword owes something to the legend of Excalibur, the sword of King Arthur, which in some legends must be drawn from a stone by the rightful king. The idea of fitness to carry the sword is echoed in the sword of Gryffindor's return to worthy members of its true owner's house.
Referred from Sword of Gryffindor
There is similar description of sword appearing for any student from Gryffindor, by former minister of magic Rufus Scrimgeour
“Unfortunately,” said Scrimgeour, “that sword was not Dumbledore’s to give
away. The sword of Godric Gryffindor is an important historical artifact, 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, whatever Dumbledore may have decided.” Scrimgeour scratched his badly shaven cheek, scrutinizing Harry.
Harry Potter and the Deathly Hallows, Chapter 7, The Will of Albus Dumbledore
1. How did the Sword of Gryffindor come out of hat.
I have a theory but it may be completely wrong. There is something similar in Percy Jackson and the Olympians. It deals with magically sheath in which weapon returns to the sheath only if certain condition apply (Spoiler Alert!).
"Anyway," Ares continued, "I tinkered with the magic a bit, so the bolt would only return to the sheath once you reached the Underworld. You get close to Hades.... Bingo, you got mail. If you died along the way—no loss. I still had the weapon."
I believe the Sword Of Gryffindor and Sorting Hat have similar connection as discussed above. Whenever a true Gryffindor needs help, the sword would appear no matter where it is kept, as long as the sorting hat is around. This explains how the sword appeared when Neville Longbottom needed it the most.
According to Pottermore
The Sorting Hat is a hat which used to belong to Hogwarts founder Godric Gryffindor.
Referred from Sorting Hat
Lets get back to the history of Sword of Gryffindor:
The sword was made to Godric Gryffindor's specifications by Ragnuk the First, finest of the goblin silversmiths, and therefore King. When it was finished, Ragnuk coveted it so much that he pretended that Gryffindor had stolen it from him, and sent minions to steal it back. Gryffindor defended himself with his wand, but did not kill his attackers. Instead he sent them back to their king bewitched, to deliver the threat that if he ever tried to steal from Gryffindor again, Gryffindor would unsheathe the sword against them all.
In the days before the International Statute of Secrecy, when wizards mingled freely with Muggles, they would use swords to defend themselves just as often as wands. Indeed, it was considered unsporting to use a wand against a Muggle sword (which is not to say it was never done).
Referred from Sword of Gryffindor
So the Sorting Hat and Sword belong to Godric Gryffindor. From the above mentioned facts,
- Godric Gryffindor might have been expecting attempts from Goblins to steal his sword (like Griphook did).
- The sword could be used for dueling with muggles.
We can assume that Godric Gryffindor might have associated the Sorting Hat and Sword, so that whenever he needed his sword, he could get it from his hat so that he does not have to worry about carrying it around and this would not allow goblins to steal the sword from him.
2. How did Dumbledore take it out ?
Dumbledore did not take the sword out of the hat. Harry had received the sword from the Sorting Hat to fight Voldemort. Dumbledore kept the sword in glass case to use it. The sword does not disappear into the hat after the work is done.