Most of the time I have seen the Doctor only with female companion. Why does he do so?
As partially pointed out by user14111, one of the roles of the companions are to provide something which the audience can identify with. As well, they were could ask questions that the Doctor (who was initially quite mysterious) needed to answer about some aspects of the science in the show.
So, originally, teenage girls could identify themselves with Susan while older people would be able to identify themselves with Ian and Barbara. According to here "They provide the lens through which the viewer is introduced to the series. The companion character often furthers the story by asking questions and getting into trouble, or by helping, rescuing, or challenging the Doctor."
For the first five Doctor's there were a mix of male and female companions and it seems from the sixth onwards that there seemed to be a move to have only female companions (Peter Davison at one time had Adric, Nyssa and Tegan and got rid of Adric partially because of the difficulty in writing for more than two companions, Colin Baker: Perry, Mel, Sylvester McCoy: Mel, Ace, .....). This may have been because of the rise of "girl power" during the late 1970's onwards in the UK?
(Admittedly, Jon Pertwee was often accompanied by one female for several stories, Liz, Jo or Sarah Jane)
It's noted in the About Time books published by Mad Norwegian Press (probably in the volume 1, but not necessarily, as they weren't published in order, starting with volumes 3 and 4) that one of the reasons to a have young(ish) male companion was that the first Doctor (William Hartnell) certainly didn't look like he'd be up to participating in intense physical conflicts. Thus, he always had a male companion up to chases and fights.
When prepping for the second Doctor, a final candidate hadn't been chosen when the needed to provide his next companions, Ben and Polly. And, while Patrick Troughton was definitely younger than Hartnell, he was just old enough that having Ben (and soon after his "renewal", Jamie) around was convenient.
By the time Jon Pertwee took over the role, his character was young enough (and willing enough!) the handle his own fights (usually with Venusian karate or jiu-jitsu). And, as noted, since a fair portion of his time in the role had his character trapped on Earth and working directly with Unit, he did have plenty of regular male allies around; it's that Liz, Jo, or Sarah Jane were there for everything, not just what happened on Earth, while they were involved with the Doctor.
Al that said - on screen, the Doctor has usually been willing to take on anyone who was interested. Turlough, for example, pretty much invited himself along (he wasn't expressly invited like, for example, Leela), but the Doctor had no particular issue with that. Especially given what's been established in-universe regarding the past several Doctors (that many companions have entertained romantic notions at some level), it's plausible that the same factor has been true for some time; that, as the Doctor has appeared to be a human male, human females have been more likely to be willing to travel with him due to feeling some sort of attraction (that, as far as I've ever seen, is not really reciprocated). This, of course, is pure supposition.