Resistance is genetically encoded and genes involved may be transmitted among bacteria. Mutations conferring antibiotic resistance and the mechanims of bacterial adaptation to antibiotics are favoured by the exposure of bacteria to antibiotics. I am enclosing a very recent and excellent review on the topic by Dr. Piddock's group.
Bacteria can develop resistance to antibiotics by mutating existing genes or by acquiring new genes from other strains or species (horizontal gene transfer).The sharing of genes between bacteria by horizontal gene transfer occurs by many different mechanisms. Mobile genetic elements, including phages, plasmids and transposons mediate this transfer.
There is normal genetic variation between bacteria, as with any living organism. An antibiotic may occasionally fail to kill all the bacteria, giving the survivors with atypical genes an advantage, so they can reproduce and become the dominant resistant strain. This process of natural selection will occur even if there are no new genetic changes or mutations, though these speed up the process.