David Miranda, who was detained at Heathrow airport for nine hours last August. Photograph: Evaristo Sa/AFP/Getty Images

Three high court judges have dismissed a challenge that David Miranda, the partner of the former Guardian journalist Glenn Greenwald, was unlawfully detained under counter-terrorism powers for nine hours at Heathrow last August.

The judges accepted that Miranda's detention and the seizure of computer material was "an indirect interference with press freedom" but said this was justified by legitimate and "very pressing" interests of national security.

The three judges, Lord Justice Laws, Mr Justice Ouseley and Mr Justice Openshaw, concluded that Miranda's detention at Heathrow under schedule 7 to the Terrorism 2000 Act was lawful, proportionate and did not breach European human rights protections of freedom of expression.

The ruling says that Miranda was stopped in transit between Berlin and Rio de Janeiro after meeting the film-maker Laura Poitras, who had been involved in making disclosures based on documents leaked by the US National Security Agency contractor Edward Snowden.

Miranda was carrying encrypted files, including an external hard drive containing 58,000 highly classified UK intelligence documents, "in order to assist the journalistic activity of Greenwald". The Guardian made his travel reservations and paid for the trip.

Laws said he noted that the seized material included personal information that would allow staff to be identified, including those deployed overseas.

Greenwald told the judges that the security services were well aware that the seized material was in connection with journalism and not terrorism. He said there was no evidence to indicate that any disclosure had actually threatened or endangered life or any specific operation.

"In my view, this is not surprising, given the care we took not to create such a risk," Greenwald said in his witness statement. Miranda said the material was so heavily encrypted that he was unable to open it.

The judges dismissed Greenwald's claims, saying there was "no perceptible foundation" for the suggestion that they were not putting national security or lives at risk by possessing the material.

Laws accepted that agreeing not to publish material simply because a government official had said it might damage national security was antithetical to the most important traditions of responsible journalism, but said this was trivial compared with the threat to security.

He said that neither Greenwald nor Miranda was in a position to form an accurate judgment on the matter because that would depend on knowing the whole "jigsaw" of disparate pieces of intelligence.

Laws said he had no reason to doubt any of the evidence from Oliver Robbins, the deputy national security adviser at the Cabinet Office, that the material was likely to cause very great damage to security interests and possible loss of life.

"In my judgment, the schedule 7 stop was a proportionate measure in the circumstances. Its objective was not only legitimate but very pressing," he said.

Miranda said he would challenge the decision. "I will appeal [against] this ruling, and keep appealing until the end, not because I care about what the British government calls me, but because the values of press freedom that are at stake are too important to do anything but fight until the end," he told The Intercept website, which is edited by Greenwald. "I'm of course not happy that a court has formally said that I was a legitimate terrorism suspect, but the days of the British empire are long over and this ruling will have no effect outside of the borders of this country."

A Guardian News & Media spokesperson said: "We're disappointed by today's judgment, which means that an act designed to defeat terrorism can now be used to catch those who are working on fundamentally important issues. The judgment takes a narrow view of what 'journalism' is in the 21st century and a very wide view of the definition of 'terrorism'. We find that disturbing."

Miranda's solicitor, Gwendolen Morgan of Bindmans, said her client had no option but to take the case to the court of appeal as the ruling meant that journalism was at risk of being conflated with terrorism. The high court turned down a direct appeal, but Miranda has the right to petition the appeal court judges to hear the case.

The ruling was widely condemned by human rights groups, including Liberty, which actively intervened in the case, but Helen Ball, the Metropolitan police's national counter-terrorism co-ordinator,welcomed the ruling. She said Miranda's detention was lawful and undertaken for pressing reasons of national security. "Some commentators have characterised the stop as an attack on journalistic freedom. This was never the case. The judgment is a clear vindication of the officers' conduct, demonstrating that they acted lawfully and in good faith throughout."

The ruling prompted strong criticism from some politicians. Former Conservative shadow home secretary, David Davis, said that when the counter-terrorism law was passed it was never thought that its powers would be used against journalists.

"There can be no suggestion that Mr Miranda was a terrorist or that he was seeking to abet terrorism, and it was for these purposes that this power was given to the politicians and the security agencies," he said.

Julian Huppert, the Liberal Democrat MP, said the ruling showed schedule 7 was too broadly drafted. "We have already made some changes to the law which are about to take effect, but I think there is still more to do," he said.

The Tory MP Julian Smith, a strong critic of the Guardian, said: "This always seemed a bizarre complaint for Mr Miranda to have made since he was transporting such sensitive information about our national security. Let's hope the full truth about the risks to which he and the Guardian continue to expose the UK is now given the full focus it deserves. That is where there is a real legal case to be made."

Rosie Brighouse, Liberty's legal officer, said: "If such a barefaced abuse of power is lawful, then the law must change. Miranda's treatment showed schedule 7 for what it is: a chillingly over-broad power, routinely misused. People are held and interrogated for hours, their property confiscated while they're swabbed for saliva – all without any suspicion that they've done anything wrong."

The reasons

The high court ruled that David Miranda's nine-hour detention and the seizure of his computer equipment was lawful under schedule 7 of the Terrorism Act 2000 because:

• Although it was "an indirect interference with press freedom", there was not only compelling but "very pressing" evidence of a risk to national security.

• It was justified because a Cabinet Office official testified that the release of 58,000 highly classified GCHQ files Miranda was carrying would be very likely to cause great damage to security and possible loss of life.

• The judges refused to recognise that the seized files were "journalistic material" and insisted they included stolen raw data that did not warrant any freedom of expression safeguards.

• The judges dismissed claims that schedule 7 was used by the police only because it avoided any need to get prior authorisation from a judge to seize material from individuals involved in journalism.