He will take over from Sir Iain Lobban when he steps down this autumn after six years as director, Foreign Secretary William Hague announced.
Mr Hannigan, 49, has been director general of defence and intelligence at the Foreign Office since 2010.
Cheltenham-based GCHQ came under scrutiny after disclosures from ex-US intelligence contractor Edward Snowden.
Mr Hague said: "As well as his impressive personal qualities, Robert brings to the job a wealth of relevant experience in the fields of national security, counter-terrorism and international relations.
"I'd also like to thank Sir Iain Lobban for his consistently strong and professional leadership as director of GCHQ since 2008."
Mr Hannigan's appointment has been agreed with Prime Minister David Cameron in consultation with Deputy Prime Minister Nick Clegg.
Gloucestershire-born Mr Hannigan, a married father of two, has advised the prime minister on counter-terrorism, intelligence and security policy for a number of years.
He said of his appointment: "It is a privilege to be asked to lead GCHQ, an organisation which is so central to keeping the people of this country safe.
"I have great respect for the integrity and professionalism of the staff of GCHQ and for what they have achieved under the outstanding leadership of Iain Lobban.
"I am excited about meeting the challenges of the coming years with them."
Mr Hannigan previously worked as principal adviser to then prime minister Tony Blair on the Northern Ireland peace process.
He was also responsible for the UK's first cyber security strategy and oversaw the first national security strategy, and has chaired Cobra emergency committee meetings on terrorist incidents.
National Security Adviser Sir Kim Darroch, who led the recruitment process, said: "I am delighted Robert Hannigan has been appointed director, GCHQ.
"He will bring energy, flair, deep knowledge and extensive experience to the role, and I look forward to working closely with him."
Sir Kim thanked Sir Iain for doing "an absolutely outstanding job at an exceptionally testing time for the organisation".
Sir Iain joined GCHQ in 1983, becoming director in June 2008.
Last November, he became the first head of the agency to give evidence in public when he appeared before MPs on the Intelligence and Security Committee, alongside the heads of MI5 and MI6.
They came under pressure to be more open after leaks by Mr Snowden revealed widespread spying by GCHQ and the US National Security Agency.
Sir Iain told the committee Mr Snowden's disclosures had done immense damage to Britain's counter-terrorism efforts.

A 40-year-old man faces charges of possessing the drug with intent to supply as well as having criminal property and concealing it.
A 25 year old faces charges including cultivating cannabis, supplying the drug and concealing criminal property,
Police said the drugs were found after a car was searched at Trench Road, Londonderry, on Thursday 22 September.
About £10,400 in cash was also seized during the search of a van on the Toomebridge Bypass at Hillhall Road.

Costa, who holds Brazilian and Spanish passports, has written to the Brazilian Football Confederation stating he wants to represent the world champions.
Brazil coach Luiz Felipe Scolari said recently he planned to select the 25-year-old for friendlies in November.
Costa played for Brazil in March - but only in friendly matches, and so could switch allegiances.
In a statement, the Spanish Football Federation said that the player has stated "his desire to be at the disposition of the Spanish national team manager Vicente del Bosque" in a signed letter addressed to Julio Cesar Avelleda, secretary general of the Brazilian Football Confederation.
This season, Costa has been in superb form for Atletico - the club he has been with since 2007 - and is the top scorer in La Liga with 11 goals after 10 matches.
"I hope people understand and respect my decision because it has been very difficult," he said.
"It was very difficult to choose between the country where you were born and the country that has given you everything.
"I looked at everything and saw that it was right and best to play for Spain because this is where I have done everything. All that I have in my life was given to me by this country."
"I hope people understand because it is not something against Brazil," he added.
"I have family in Brazil and it is the country where I was born. I hope that God allows me to live there again in the future."
Should he be selected, he could make his Spain debut on 19 November in a friendly against South Africa.
Robert Hannigan has been named as the new head of the UK's electronic surveillance agency GCHQ.
[NEXT_CONCEPT]
Two men have been charged in connection with the seizure of cannabis worth an estimated £280,000 in Londonderry.
[NEXT_CONCEPT]
Atletico Madrid's Brazil-born forward Diego Costa has confirmed his intention to play for Spain.