Ordinarily, its the governors
Lucius Malfoy (who, at the time, is a governor) confirms this in Chamber of Secrets:
"The appointment — or suspension — of the headmaster is a matter for the governors, Fudge,” said Mr. Malfoy smoothly.
Harry Potter and the Chamber of Secrets Chapter 14: "Cornelius Fudge"
Apparently the Ministry can too
Of course it's a little hard to reconcile this with Umbridge's appointment, which appears to be decreed at the will of the Minister and is announced in an Educational Decree signed by Fudge, not by an Order signed by the twelve governors. Based on the conversation between Fudge and Dumbledore at Harry's trial, however, it seems as though the Ministry may have given itself the authority to intervene at Hogwarts, though they couldn't before; the relevant passage is:
[A]s the Ministry has no authority to punish Hogwarts students for misdemeanors at school, Harry's behavior there is not relevant to this inquiry," said Dumbledore, politely as ever, but now with a suggestion of coolness behind his words.
"Oho!" said Fudge. "Not our business what he does at school, eh? You think so?"
"The Ministry does not have the power to expel Hogwarts students [...] In your admirable haste to ensure that the law is upheld, you appear, inadvertently I am sure, to have overlooked a few laws yourself."
"Laws can be changed," said Fudge savagely.
Harry Potter and the Order of the Phoenix Chapter 8: "The Hearing"
So at least as of Order of the Phoenix, it seems that the Ministry also has the authority to appoint Head Teachers; how often they have done so, or if they retained that authority after the end of Deathly Hallows, is unclear.
However they don't appear to have the authority to demand that the Castle accept her as Headmistress: although she's accepted (grudgingly in many cases) by the staff and students, the Head Teacher's office and the magical paintings of prior Heads do not. This isn't altogether surprising, though, since the fundamental forces of nature are notoriously hard to legislate.