Some bacteria are autotrophs, some obtain energy from the sun, and other bacteria break down chemicals and turn it into energy much like what plants do
Bacteria produce enzymes to break down dead organic matter eg carcus, plants, also some that are inside you, intestines etc. eg. e.coli steal your food ;)