Geoff Hoon, the defence secretary, announced a new code of service conduct covering all personal relationships - heterosexual and homosexual. He told the Commons the ban on gays was no longer legally sustainable following the court's judgment in September that sexual orientation was a private matter.

The new code will apply regardless of rank, gender, or sexual orientation. At its heart will be what the ministry of defence calls the "service test" set out in the form of a question: "Have the actions or behaviour of an individual adversely impacted on or are they likely to impact on the efficiency or operational effectiveness of the service?"

The code, which will be applied at the discretion of individual commanders, is based on the principle of non-discrimination and the recognition that different standards of public behaviour, on flouting sexuality for example, are needed for the armed forces.

It states: "The overriding operational imperative to sustain team cohesion and to maintain trust and loyalty between commanders and those they command imposes a need for standards of social behaviour which are more demanding than those required by society at large."

As examples of behaviour which could undermine such trust and cohesion, it cites "unwelcome sexual attention in the form of physical or verbal conduct" and "displays of affection which might cause offence to others.

"It is important to acknowl edge in the tightly-knit military community a need for mutual respect ... each case will be judged on its merits."

Stonewall, which campaigns for gay and lesbian equality, welcomed the new code which, it said, should provide protection against sexual harassment, discrimination, and bullying. Angela Mason, its executive director, described it as "absolutely sensible". The ban on homosexuality in the armed forces had been "a great symbol of inequality".

Steve Johnston, chairman of Rank Outsiders, set up to support those dismissed from the services for being gay, said: "We will closely watch the implementation of the ruling and we are keen to work closely with the MoD to ensure equality and fairness for all."

But Peter Tatchell, of Outrage!, the gay pressure group, said that "deeply entrenched homophobic attitudes will continue to force many gay people out of the services, unless the military authorities take tough new action to combat the victimisation of homosexual personnel".

Acknowledging resistance in the services against lifting the ban, Mr Hoon said: "There will be those who would have preferred to continue to exclude homosexuals but the law is the law.

"We cannot pick and choose the decisions we implement. The status quo is simply not an option."

Navy commanders are likely to be most concerned - they have argued forcefully in favour of maintaining the ban.

Iain Duncan Smith, the shadow defence spokesman, attacked the decision. "I believe and have always believed, as the previous government did, that we should follow the advice of the armed forces which has always been that lifting the ban would adversely affect operational effectiveness."

Women will still be barred from some front-line duties.