As a child in Uganda, John Bosco remembers hearing an old wives' tale that if a man fell asleep in the sun and it crossed over him, he would wake up as a woman. "I used to try that as a kid," says John now, some 30 years later. He sits at a table in a busy cafe across the road from the railway station in Southampton, his fingers playing with the handle of a glass of hot chocolate. "I'd spend all day lying under the sun. From childhood, I wanted to be a girl. I wanted dolls. At school, I played netball. I wanted to dress up like a girl … I rubbed herbs into my chest that were meant to make your breasts grow. I tried everything but it didn't work."

He tells me that there was not one single moment when he realised he was gay; that the knowledge of it had always been there, unexpressed until he found the right words. As he grew older, John started being attracted to men. On the radio, he heard stories of gay couples being beaten and killed by police. He says that if he could have changed himself, he would because he so desperately wanted to be considered "normal", to fit in, to make his family proud.

When he went to university to study for a business administration degree, his relatives and neighbours in Kampala would ask why he never had a girlfriend. "I used lots of excuses – I'm not yet ready, or I have a girlfriend who doesn't live in the same area," he says. "It was difficult because you cannot be open [about your sexuality]. You can't socialise like any other person. A lot of the time, you have to keep your distance. You feel you're not yourself. It makes things really hard."

This is the reality of being gay in modern Uganda, a place where homosexuality is criminalised under the penal code, punishable by life imprisonment. According to human rights organisations, about 500,000 homosexuals live in the country, unable to admit their sexuality for fear of violent retribution either from the police or their own communities. Anti-gay legislation is a relic of British colonialism, designed to punish what the imperial authorities thought of as "unnatural sex" – thinking that was subsequently reinforced by wave upon wave of Catholic missionaries.

Although much of that legacy has been dismantled as Uganda modernises, homophobia is as entrenched as ever. An anti-homosexuality bill, due to be discussed by parliament before June, advocates the death penalty for "aggravated homosexuality" –ie for gay people with HIV practising sex, or gay people who have sex with someone under 18. Known colloquially as the "kill the gays" bill, it would also make it a crime not to report someone you know to be a practising homosexual, thereby putting parents, siblings and friends at risk.

"One of the things the Ugandans say is that being gay is European culture, that it is un-African," explains John, 31. "There is this idea that Europeans and Americans are recruiting people to be gay, giving them money to do it."

Last October, the now defunct anti-gay Ugandan tabloid Rolling Stone published a list of the country's "top 100" homosexuals under the headline "Hang Them". In January, the prominent gay-rights activist David Kato was murdered – beaten to death in his home by a hammer-wielding thug. Gays, lesbians and transgendered people in Uganda face harassment, extortion, vandalism, death threats and violence on a daily basis. They can be sacked from employment if they are outed, forced to enter into heterosexual marriage and detained by the authorities without charge or access to legal defence. In some of the worst cases, they can be subjected to so-called "correctional rape".

It is not only Uganda – for years, the developed world has turned a blind eye to the state-sanctioned persecution of homosexuals that exists in 38 out of 53 African nations, according to Human Rights Watch. Now, a new feature-length documentary film seeks to redress the balance. Getting Out, directed by film-maker Alexandra Chapman in conjunction with Christian Aid, tells the story of the gay refugees who are forced to flee discrimination in their own countries.

"It is very important for people in the west to understand that legalised and state-sanctioned homophobia is a reality in many parts of Africa," says Dr Chris Dolan, director of the Refugee Law Project at Makerere University in Kampala, who was instrumental in the making of the film. Dolan, who campaigns extensively to protect the rights of beleaguered minorities in this corner of Africa, says that the political climate in Uganda "enables a wide range of abuses and violations that seriously diminish the quality of life of all lesbian, gay, bisexual, transgender and intersex persons, most of whom seek to stay under the public radar. It also places many such persons in serious and extreme danger."

For John, the danger soon became too great to ignore. At his university freshers' ball, he met and fell in love with a man called Aziz. The two of them were discreet, taking care not to be seen acting too intimately in public. In this way – never quite being honest, living in the half-shadows, always looking over their shoulders – their relationship continued after graduation when John took a well-paid job in a bank. When John first took Aziz home to visit his family, he was introduced as "my best friend. He became like another son to my mum. That was the way it was until 2001."

Then everything changed. A group of John's gay friends were arrested in a police crackdown. They were beaten and forced to give the names of other gay people they knew. John realised he had to get out. "I had to disappear," he says. "I had some money saved up so I paid a private agency to get me a visa, a passport … I didn't tell anybody I was leaving, not even my family. At first, I didn't know where I was going. But then, luckily, the guy gave me a visa to the UK."

John Bosco did not know it then, but his problems were only just beginning.

Florence Kizza smiles a lot. She has a sharp, pretty face with slanted eyes and straight, white teeth. When she talks, she does so in an even, clear voice, her faint Ugandan accent lending the words an irregular rhythm. We meet in a cafe in Richmond, Surrey, near to where she works as a bank clerk. Although the story she tells me is a horrific one, Florence does not show emotion as she recounts it, beyond a slight narrowing of the eyes, a glance to one side, a short pause in her narrative. She explains that to break down and cry would be to give into something she needs to resist. Because Florence is a woman who defines herself by her survival.

Florence is 32, Ugandan and a lesbian. She grew up in Najjanankumbi, on the southern edge of Kampala, the daughter of a prosperous businessman who sent Florence and her sister to a prestigious girls' boarding school.

"I kind of knew [about my sexuality] at school, but those things you don't talk about," she says. "It's something you never breathe out loud. I was brought up a Catholic. Every day, these pastors are preaching that a gay person should be stoned to death, that they should die. If you heard that, would you be open?"

When Florence was 16, both of her parents died of Aids within a year of each other. Florence was taken out of school and raised by relatives. The older she got, the more certain she became that she was gay. Lonely and increasingly isolated, she craved companionship. And then, buying food at the market one day, she met a woman called Susan, from the west of the country. "She spoke a different language," says Florence, "but we just connected. We went for coffee, we talked and then we met up five more times." Gradually, the two of them became closer but, like John Bosco, they were careful about how they acted together in public. Florence continued to live alone. Still, the fact that she was a woman of marriageable age without a husband aroused the suspicion of the local community.

In December 2000, neighbours broke into her house and found her in bed with Susan. The villagers stripped the two women naked, paraded them through the streets and then beat them in front of a baying crowd. "To say it was painful is an understatement," says Florence now. "You can take being hit but being humiliated around God knows how many people – you lose your dignity. I felt, I wish I could die now."

Banished from her village, Florence was forced to find somewhere else to stay. She spent the nights at Susan's home, waking up early each morning to sneak out under cover of darkness. But however cautious she tried to be, it was never enough. In March 2001, Florence was arrested and, over a three-day period, was beaten and raped by three policemen at gunpoint. The assault was so ferocious that, 10 years on, Florence still bears the scars. It is cold when we meet and Florence is wearing a long-sleeved zip-up sweatshirt but, even in the milder weather, she does not like to show the twisted ridges of skin that snake all the way up her arms.

"Looking back, I think the police officers found me very challenging," Florence says, and she half-closes her eyelids, as though squinting to make out a murky, distant shape. "There was a time when one of them hit me on the second day and I looked at him and I didn't cry. I looked very, very calm. I told him: 'Have you finished? It doesn't hurt,' and I laughed." She looks up, meeting my gaze. "And he stopped."

On the third day, Florence escaped when one of the policemen fell into a drunken stupor and she was able to steal the keys to her cell. She ran out into the streets and got a taxi to a friend's house. She knew she had to get out of the country before the police tracked her down. She and Susan fled across the border to the Democratic Republic of the Congo. There, they paid a human trafficker to take them to the UK. "He said, OK, you have to pay a very big price. He asked for £20,000. I had to give up one of my dad's plots of land as security."

At the last minute, the trafficker said it was too dangerous for both Florence and Susan to travel at the same time. "He said, it's one person, you choose. I said Susan should go because I was feeling ill, I didn't have the energy. But they said I should go because my health was bad and I was the worst off."

In September 2001, Florence flew to the UK and was taken by the trafficker to a B&B in Wembley, north London. He gave her a £50 note and left her there. At the age of 22, Florence was on her own in a sprawling foreign city with little money and no prospects. For days, she walked the streets, unsure of what to do or who to turn to. After her experiences in Uganda, she looked at everyone with mistrust and suspicion. She had to beg for money for food.

A man from a local church group eventually took her to the Home Office to seek refugee status but Florence was deeply intimidated by the interview process. "Basically, I didn't trust authorities because of the bad experiences I had with them in Uganda," says Florence. "The interviews were degrading. They would ask me to talk about my personal life, to explain how I had sex. The way they looked at me, I just thought, Jesus Christ, am I this disgusting? Honestly, I was so angry. They just had no idea."

Already suffering from post-traumatic stress disorder and depression, Florence's symptoms got worse the further she travelled through the UK appeals system. Her initial application to stay was refused, on the grounds that she would be safe if she returned to Uganda, relocated to a different area of the country and acted "discreetly".

According to Erin Power, the group manager of the UK Lesbian and Gay Immigration Group, a large part of the problem for gay Ugandan refugees is an unwillingness to talk openly about a sexual identity that they have had to keep secret all their lives, often even from themselves. "If they do what they're supposed to do and approach the Home Office as soon as they land in the UK, you're asking them to go up to a figure of authority in a uniform and tell them they're gay," says Power. "But that is the person who, in their country, will persecute them for speaking openly.

"We have clients who have never said they're gay before. The idea that they can identify themselves is problematic because often they have kept it secret all their lives … Some clients have never had sex, but we argue that being LGBT is not about who you have sex with, it's about who you are and what your identity is. We've struggled to get the Home Office not to focus on sex. Up till now you've had to prove two things: one, that you're gay or lesbian and two, that your country's dangerous."

How do you prove you're gay? Power laughs. "Everyone always asks that."

John Bosco was facing similar problems in a different part of town. Having arrived in London with £600, John found a room to rent in Manor Park, north London. "I thought, if I can get to an English-speaking country, I'll be OK. As soon as I get there, I can get a job because I have qualifications. I didn't know the asylum system at all."

When he tried to apply for jobs, he was told he needed a national insurance number. "I didn't know what it was," says John. Eventually, a group of Jamaicans he met on the street directed him to the UK Border Agency offices in Croydon. But instead of what he thought would be a straightforward interview, John says he was stripped naked, asked for his fingerprints, bundled into a van and taken to the Oakington immigration detention centre in Cambridgeshire.

Here, he spoke to the authorities through a translator, but the interpreter was from a different part of Uganda and did not speak the same tribal language so John's statement was littered with inconsistencies. John, terrified as to how the UK authorities might react, did not tell them that he was gay and that this was the real reason he had fled Uganda. "They asked me if I wanted a solicitor," he says now, shaking his head. "I didn't know what this word meant."

Failing to make himself understood or to provide a consistent story to explain his refugee status ended up costing John dearly. From Oakington, he was taken to Haslar, an immigration removal centre run by the prison service in Portsmouth. For the first few weeks, he had no change of clothes and had to wash his single pair of underpants every day. When a local volunteer visited him to ask if he needed any help, John finally confessed everything.

"When she asked me, 'Why did you leave?' I said because of my sexuality. She said: 'That's OK, that's not a problem.' I had to sit back like this." He leans back in his cafe chair, crossing his arms over his chest with an expression of shock in his eyes. "I was shivering. I'd never had anyone talk to me like that. She was the first person I'd ever told about my sexuality and she was nice to me." He breaks off, bows his head and rapidly wipes his eyes.

After four months in Haslar, John was given leave to stay in the UK but the Home Office appealed against the decision. For the next six years, from 2002 to 2008, John's life became an exhausting cycle of legal battles. He got a job working at a mental health charity in Southampton and poured £21,000 of his own money into solicitors' fees. In 2008, during a routine visit to the police station (the terms of his leave to remain in the country required that he report to the police once a month), he was manhandled into a van, taken to the airport and put on a flight back to Uganda.

"I was thinking, just kill me. I have no friends, no relatives, nothing. How long is this going to go on? I'm not going to change myself to be accepted."

As with Florence Kizza, the judge in charge of his case had decided that John would face no immediate danger if he returned to Uganda, changed his behaviour and moved to a different part of the country to live "discreetly". This was in spite of the fact that John's photograph had been printed on the front page of a national newspaper in Uganda only a few weeks before he was deported. Living discreetly was just about the last thing he could do.

Within days of touching down in Kampala, John was arrested. The police threw him into a cell with several other inmates and subjected him to regular beatings. "The beatings are not something you can say you get used to," he says now. "It's something you expect."

He bribed the police to release him with the little money he had left and went into hiding for six months. In the end, his solicitors won him refugee status for five years and he was flown back to the UK. But the leave expires in 2014 and John still lives in a state of anxious uncertainty, isolated from his family, friends and his former boyfriend Aziz, all of whom he has found it impossible to trace.

"I have bad dreams still: people chasing me, being beaten up," he says. "Sometimes I sleep and then I think, what will happen after 2014? All I want is freedom, where I can be who I am."

Florence was granted permanent refugee status last year. Since leaving Uganda, she has completed a degree in business management at Kingston University. For a while she worked for a supermarket; now she has a job in the offices of a high-street bank in Twickenham, London. She never heard from her girlfriend Susan again. "We tried really hard to locate her," she says, her voice drained of emotion. "I think I'm getting used to it."

In July 2010, the UK's Supreme Court categorically denounced the "discretion reasoning" that had been central to the rejection of both Florence's and John's refugee claims, ruling that the decision failed to recognise the human rights of homosexuals and breached the UN refugee convention. The Home Office has since produced a set of guidelines, in consultation with asylum groups, on how to assess the validity of such claims, and all senior case-workers have been put through a one-day training session on the connected issues. "That process finished at the end of February," says Erin Power, "so we don't know what the outcome will be. Obviously we hope there will be some improvement because some of the interviewing was horrific, quite honestly."

Back in the cafe in Southampton, John's hot chocolate has gone cold. He says he misses his family "all the time" and does not have much of a social life, feeling too black to be fully welcomed by the predominantly white gay community in this part of the world, and too gay to be fully accepted by the straight people he meets. He spends most of his evenings and weekends in a rented room watching TV soaps. "Calling the memories back stresses me out," he says, at the end of our conversation. "But the reason I do it is because if I don't, people won't understand what is happening, especially the people in Uganda who do not have a voice. The only way they will understand is for me to tell you about it."

As he pushes his chair neatly under the table, he says that he is plagued by two questions. "I ask myself all the time, why was I born gay? And if I was born gay, why was I born in Africa?"

He leaves, letting the cafe door slide silently shut behind him, turning back to give me a wave and a smile through the window as he goes. Perhaps there will never be an answer. But for now, at least, John Bosco is free to pose the questions out loud.

Getting Out will be shown on Friday 15 April at 7pm at The Frontline Club, 13 Norfolk Place, London W2 1QJ. Tickets are free but space is limited. To reserve a seat email admin@uklgig.org.uk