For starters, a terrarium has its own water cycle: since no water can escape, the same water molecules keep knocking around in that bottle, getting sucked up by the plant’s roots and then transpired out of its leaves, condensing on the sides of the container and running back down the sides and into the soil.
The plant is kept alive much the way all plants in ecosystems the world over make it work. Aerobic bacteria from the compost eat the dead plant matter, making more nutritious soil for the plant to use. The bacteria also use up the oxygen released from plants and transform it into carbon dioxide, which is necessary for the plants to photosynthesize.