
Aaron B. answered 10/02/19
PhD in biochemistry, with significant emphasis on molecular genetics
Two ways to get additional genes are gene duplication events and horizontal gene transfer. Adding new genes does not (necessarily) cause infertility. You are likely thinking of species having an odd number of chromosomes, which generally causes sterility.
In gene duplication, due to either an error in gene replication or other event (integration of an mRNA even) a gene is duplicated in the genome. This often frees the second copy from the selective pressure it was under, allowing it to evolve to perform other functions. It could also allows both copies to evolve to be more specialized, especially in the case of a parent gene that had multiple functions (as an example, the RNA polymerases in plants are thought to have arisen in this way http://symposium.cshlp.org/content/75/285.full.pdf).
In horizontal gene transfer, the new gene comes from another organism. One way for this to happen is through viruses (as an example https://www.cell.com/cell/fulltext/S0092-8674(17)31504-0). It can also happen between other interacting organisms.