When an apple is sliced open, the flesh exposed to air turns brown due to a series of biochemical reactions called enzymatic browning.
The flesh inside an apple is made up of tiny cells containing, among other things, naturally-occurring polyphenol molecules and an enzyme called polyphenol oxidase. When an apple is cut or bruised, some of the cells are broken open, allowing the oxygen in air, and the polyphenols to come into contact, triggering an oxidation reaction driven by the enzyme.
The initial reaction produces compounds called o-quinones, which then further react with amino acids in the apple to form brown-colored melanins. While it may look unappealing, the brown coloration is not harmful.