Maybe you are asking where these genes came from and how they became so common?
It turns out that antibiotics are very common - many free living plants and microorganisms make antibiotics. Penicillin, the first beta lactam antibiotic found is synthesized by a fairly common mold, they kill bacteria by inhibiting the synthesis of the cell wall.
All three of these classes of defense have evolved over the hundreds of millions of years of evolution since antibiotics like penicillin started showing up (probably more like billions of years). They have all probably been with us for quite a long time.
Beta-lactamases, which enzymatically neutralized the beta lactam ring of penicillins and related compounds seem to have evolved three times and its not hard to imagine why the genes have stuck around for so long.
dd-transpeptidase, the 'penicillin binding protein' is actually an enzyme that helps to synthesize the peptidoglycan of the cell wall. It can also mutate such that it tends not to bind penicillin so well, creating a second sort of penicillin resistant bacterium.
Other genes such as ABC drug transporters can kick out strange molecules before they can cause any damage.
These and all other sorts of mutants and genes that convey resistance, once created through selection are around in the huge bacterial gene pool. Bacteria can pick them up through lateral gene transfer, plasmids (sex), phages and by taking in DNA from the environment. Most of these genes and traits were sitting around for millions of years, but the increased use of antibiotics has increased the selection pressure to retain these traits in many more bacteria. That's really where bacterial antibiotic resistance comes from.