The underlying reason for this conundrum was first outlined by Ronald Fisher and is still known as Fisher's Principle. It doesn't just hold true for mammals, but for most sexually reproducing organisms.
Natural selection favors things that help individuals, not species. Something can be great for a species or terrible for a species, but that won't effect whether selection favors it. Instead, it favors things that help individuals, even if those traits are less than optimum for the species as a whole.
So, why have equal numbers of males and females? Well, basically, imagine a situation where 100 females were born for every male. Now imagine some individual has a mutation that causes them to produce more male offspring, instead of just having 1 in 100. All their male offspring will do very well. They'll go on to impregnate dozens of females and produce lots of offspring. The gene for having more males will spread. The gene for having more females will not keep up, because individuals with that gene will only produce females, and those will only produce a few offspring. Eventually, though, there will be lots of males. At that point, it will be the males who have trouble finding mates, while all females are guaranteed a chance to get lucky. And so production to produce more females will be favored. This all evens out to favor a nearly 50-50 sex ratio at birth. Basically, selection favors having offspring that have the most opportunity to reproduce, and this means having offspring of the less common sex. This is known as negative density dependent selection.