First and foremost, not every type of tree loses its leaves. Only deciduous trees lose their leaves. There are two important factors related to this process: light and temperature.
In many cases, deciduous trees grow in areas that experience cold, harsh winters. Trees drop their leaves to shield themselves during these winter months, because the dry cold winter winds will cause the trees to lose their moisture and the leaves have a large surface area. By shedding their leaves, the trees can preserve the moisture in their branches and trunk, instead of drying out and dying. Also, a tree without leaves is in a state of dormancy and needs less energy to remain alive.
In the spring and summer, leaves photosynthesize the ample sunlight that falls on them. This produces chlorophyll, which causes the green coloring. Photosynthesis generates energy for the tree, and the tree takes in nutrients from the soil to feed its leaves and keep them healthy. The warm and bright colors that the leaves take on in fall have actually been present the whole time, but they have been masked by the chlorophyll. As the days get shorter, the trees receive less sunlight, and therefor produce less chlorophyll. Then they fall off, because the tree does not have the energy to support them through the winter.