The methods astronauts use to breathe in space depends on the mission, but they usually use either electrolysis of water or pressurized oxygen from tanks that is pumped into the atmosphere. Space suits and the interior of space craft are pressurized, allowing astronauts' lungs to function normally.
On early missions like the Apollo program, astronauts relied primarily on pressurized oxygen canisters. However, the relative permanence of the International Space Station required a different solution. Astronauts on the ISS use electricity from the station's solar panels to separate the oxygen and hydrogen molecules of water, creating oxygen that is then pumped into the station. The excess hydrogen is vented into space.
The ISS astronauts have backup options in case something goes wrong with the electrolysis equipment. They keep a supply of oxygen tanks available. These tanks are also the source of oxygen for space suits, which are used during space walks as well as shuttle takeoffs and landings. Finally, astronauts have an supply of perchlorate candles. These are actually metal canisters full of lithium perchlorate. When the astronaut pulls a pin, it ignites a chemical reaction that produces oxygen.
The air is kept clean and breathable by monitoring machines and filters, which clean excess carbon dioxide and other waste gases from the air before they can build up to toxic levels. These waste gases are also vented into space.