The Thingathinga family live in a corrugated iron shack with no sanitation among thousands of other brightly painted corrugated iron shacks on the outskirts of Cape Town. Ten people, five of them children, share three dark rooms and nobody earns any money. They are among the poorest people on the planet. Last week, the richest man in the world walked in through their door.

Bill and Melinda Gates, with an air of slight embarrassment, sat on a low wooden bench in the middle of the dark room, surreally reminiscent of nervous interviewees on a breakfast TV sofa. Before their arrival, Nkosepaca, the 60-year-old head of the family, had hauled himself across the floor and into a makeshift wheelchair at one end of the bench. He lost both legs above the knee when he fell off a crowded train a couple of years ago, and the stumps were tied up with filthy rags. Gates, whose personal wealth exceeds $40bn (£22bn), sat next to him, hands in his lap, eyes lowered below his baseball cap and feet wedged behind one of the chair's wheels, which might once have belonged to a bicycle.

How were they to make conversation? Bill and Melinda Gates, whose charitable foundation takes as its premise that all lives have equal value, struggled to connect. They were there to talk about tuberculosis, because the foundation is putting millions into research to replace the ancient and inadequate BCG vaccine and find new drugs to shorten the six-month treatment time. Nkosepaca has had TB four times, infected by different strains of the bacterium - something which it later appeared had fascinated Bill Gates, who was to raise it with scientists again and again, asking what the implications were for a vaccine.

But faced with the man, he was silent and it was his wife Melinda who tried politely to engage Nkosepaca about his health, and who lit up with real warmth as she caught the eye of a wild-haired, fidgety granddaughter or a big-eyed baby. When his turn came to ask a question, Gates, looking less than comfortable, resorted to numbers.

"How many people live here?" he asked in that staccato voice that carries all the feeling of a computer chip, followed by: "How long have you had electricity?" (The answer was six years.)

The Gateses were on their first tour in South Africa since Bill announced he would step aside from Microsoft (although only reducing his involvement, he says) in 2008 and the billionaire financier Warren Buffet announced he would give the $30bn foundation most of his fortune - effectively doubling it in size.

The family had no idea of the vast wealth at Gates's disposal. "Do you know who he is?" I asked them. They shook their heads. "Or why he has come?" No clue. But as most destitute Africans reasonably do, faced with a white, well-fed foreigner, Nkosepaca asked him for help. "He asks if anybody can help us because the money we're getting is too little to sustain a family," translated a young man from the Desmond Tutu TB Centre at Stellenbosch University, which had arranged the visit. Later, one of the daughters spoke up. "I just want to know whether you can help our father," said 25-year-old Kutala quietly in English from the back of the room.

"We came for a visit," answered Melinda. "We certainly will do something to help your family because you have been so hospitable today."

If they chose, they could propel this family and many like them into prosperity with a nod. They could buy them food, a new house, clothes, education for the children and never notice a difference in the bank balance. It's an instinctual reaction, they say, but it's not the right one. Gates calls it "a kind of a retail approach where you say, 'OK, just the people I've seen - I'll help them.' It's like saying, 'OK, the ones I haven't seen don't matter - just the ones I've seen matter.'"

"We do a special gift for the houses that we go into, but that's more out of courtesy. It isn't how you can change the basic phenomenon that we've got here."

Melinda agrees, but with an emotional underlay. "I don't think you ever go into a place like this and leave without thinking about the individual. I've gone into some of the orphanages where you'd like to take all the children home with you. But then you have to always try and upscale from there and say, 'OK, if I help just that one child, what am I doing for the entire cause?'"

Gates has the ability to ring-fence a problem and focus his formidable mental energies on solving it. He moves it into a detached dimension, where he can be scientifically rational. Emotion does not get in the way. Confronted with the poverty and suffering of individuals, most people do not feel like that. In the little shack, as the Gateses run out of questions, Nulda Beyers, the professor at the Desmond Tutu TB Centre who has arranged the visit to the township of Khayelitsha, begins to prompt Nkosepaca. There are things that matter to her and that she clearly thinks Bill and Melinda ought to know. In the room are three young women with four small children between them. "Where are all the men?" she asks the old man.

"All the boys are on the street. They won't support their kids," he answers.

She pushes it further, with real feeling in her voice. "It's a big problem. The men run away. What advice can you give us for your daughters and their children? How can we make the men more responsible?"

"The men must support their kids. They are a gift from God," comes the answer.

Gates has not been following. "Where are the men? Are they gone?" he asks. The question relates to the messy world outside the clinical parameters of the development of vaccines and drugs for TB. But Melinda is there. "They won't come here because they have to face up to the fact they have to support the family?"

Of course, the Gateses know the social and economic realities of life in townships. Gates recalls his visit to Soweto, where he took a computer and then realised that the failure of electricity to power it was not the most urgent issue in people's lives. The pressing problems beyond the scientific challenges of new medicines have led the couple to set up a global development programme, but he does not have a clear vision of where it is going.

"In health, there are incredible solutions like vaccinations where, with an upfront investment, you can dramatically change a disease. In some of these other issues about jobs, motivation, education, it's not as clear what the dramatic impact is in those areas, but certainly we're thinking about it," he says. Clean water and improving crops - "so many people talk about being hungry" - are two of the items on the agenda.

He prefers philanthropy performed with technical discipline; altruism run like a multi-billion-dollar business. It will probably get results - perhaps spectacular results - but Gates is never going to be a crowd-pleaser.

Two days later, the relatively modest Gates entourage is swallowed by a whale. Bill and Melinda's fact-finding trip links up with Bill Clinton's Africa tour. Clinton is doing five countries in seven days. It's Wednesday, so it must be Lesotho - the tiny state within a state whose tribe held out in the mountains when the British took the rest of South Africa. It is proud and it is dirt-poor and suddenly there are three huge private jets on the Maseru airstrip - probably more planes than have ever been there at one time before.

Ex-presidential philanthropy looks different from that of software billionaires. Gates has his own private plane, naturally, but Clinton has two bigger ones, loaned by people richer than himself. And his millionaire backers come along for the ride, bewitched by the Clinton charisma but with nobody much to talk to. The small man with prematurely white hair and dark glasses is Frank Giustra, the Canadian financier and head of Lion's Gate Entertainment. The big man in the vibrantly striped shirt is Karl Heinz Körgel, a German media mogul - though his pilot needs some sleep, so the 15 or more journalists and camera crews following Clinton, together with a more modest four writers accompanying Gates, are temporarily on a plane leased from the president of Djibouti. It has a double bed at the back and sofas with seat belts that are mostly ignored and as we take off, Ira Magaziner, who runs the global health side of the Clinton Foundation, is perched on the side of an armchair. We sit on the table or floor to hear his briefing and catch cans of cold Coke before they slide away. There is something of a party atmosphere.

The good humour lingers on as an extraordinary cavalcade of 10 or more white cars with tinted windows and police escort screeches across a parched landscape of failed maize crops and dry grass, with hardly more than a few handfuls of bemused local farmers to notice. Lesotho has one of the highest rates of HIV infection in the world, at somewhere between 23% and 31% of young adults. We are headed for an Aids clinic which, with Clinton Foundation backing, is now offering drug treatment not just to adults but also to children. Few disagree that Clinton already has made a significant difference to Aids treatment, using his name and standing and the expertise he can command to force down the price of drugs.

And suddenly there is the former US president, in the middle of a crowded courtyard at Mafeteng hospital. Clinton has emerged from a private meeting with a small girl twisting and spinning from his hands. Arriet, six, is the first child to receive antiretroviral drugs here. She has been on them for eight months, is clearly very well and Clinton knows exactly how to handle her. She does not want to talk but gets interested in my camera, so I let her look through the viewfinder and then take her picture, while Clinton bends down to get in the shot. I show it to her and she laughs and laughs.

It is very well done. Even his security guards, the bristling, dark-suited heavies with earpieces who talk into their sleeves, melt away around the former president so you are left as if alone with him. Clinton is relaxed and warm. He lingers; he has time for everyone. He seems to have a genuine liking for people. He gives a speech of thanks at the clinic and all around people are loving him, while Bill and Melinda stand alongside, fading into the background.

You believe that Clinton cares. The focus on the human, on the individual, the stories about the brother and sister in the Bahamas for whom he got Aids drugs and whom he visits every year tell you that. And he banks the adulation and approbation to use as leverage later on.

Gates now shares platforms with world leaders, but you sense he talks numbers with them. So many lives potentially saveable. So many millions for a vaccine. He does not do human and he does not want to do politics. "Politics is a dangerous word," he says. "We're involved in working with governments to talk to them about how rich governments can make their aid money be used more effectively and encourage them to do more aid. We're involved with developing-world governments in terms of trying pilot programmes and, when something works, encouraging them to replicate that. I make a distinction between that and politics."

Some might argue for a moral imperative to get into the political argument in South Africa, where the health minister supports lemon and garlic as a cure for Aids and millions are set to die while the treatment plan is slowly, grudgingly rolled out. Gates says what has been achieved so far is due to the activists and the press. But he is meeting the deputy president privately later that day and is waiting to hear whether he will be talking to President Mbeki in person or on the phone.

"Hopefully we say good things and get them excited about doing more on these issues," he says. "Any ideas we have about the way things can improve, we will share."

"We're not the activists," Melinda goes on. "The press is the press and the activists are the activists. In private we can share whatever thoughts we might have."

Privacy matters. The Gateses will do what they have to do on the public stage to advance Microsoft or the foundation, but beyond that they do not seek attention. Their philanthropy is a family ideal, handed on from their parents. They intend that their own children should understand what it means to be less equal. Their two older children were in South Africa with them, hidden from the press. On the day after the visit to the Thingathinga family, the Gateses took 10-year-old Jennifer and seven-year-old Rory to see the crowded corrugated iron homes of Khayelitsha for themselves. "We talk at the dinner table about these issues. We think as a family we have a responsibility to give back to the world," says Melinda. If the children want to participate in the foundation, well and good, she says, but "I hope that when they grow up they will follow their own passion. They need to lead their own lives."

She talks about feeling great "as a mom" when she sees tiny babies whose lives are safer for the vaccines the foundation has funded. Gates compares the human body to a computer system. "The human body is the most interesting system," he says. "It is the most complex system." He has been reading books on the immune system since he was 32, adds Melinda, with the faintest whisper of humour. While the best job he could have in the world is the one he has, says Gates, the second best would be discovering new medicines. "It is a field that is changing. You get new tools all the time. You can have a huge impact. The kind of work and thinking that goes on is very like software," he says.

He is excited, energised, by the ideas of brilliant scientists who must think him a 21st-century messiah. Who else is going to fund their money-losing, world-saving ideas? Gates says what would be unthinkable at Microsoft. "We can afford to have a lot of failures. We're going to have a lot of failures. I will not stop working on malaria, TB or Aids because of failures."

The Buffet money means that he can gamble more millions than he expected. Extraordinarily, he is capable of counting the pennies one moment and throwing vast sums at unpredictable prospects the next. He asks prices, wants to know whether they take credit or just cash behind the wire netting in the Khayelitsha shops. But on arguably the hardest scientific challenge in medicine today, which could easily cost him billions, he says: "We're not going to give up working on an Aids vaccine. Not in my lifetime." And when I ask if he could reach a point when he will decide that too much money has been spent with nothing to show for it, he answers with what passes for a laugh: "Ask me in 30 years' time."

Regardless of anyone's views on Microsoft's business practices, it is an attitude that has to command respect. As he says himself, no government facing election every four years would take such risks. He is not standing in for rich governments - he is doing what they do not dare to do. If this is what philanthropy is about in our times, perhaps we should just be glad.