Ecology is the study of living organisms in their environment. An ecosystem is a distinct area within earth's environment and it includes organisms living there and interacting with one another as well as with the background non-living parts of the environment.
Green plants are known as PRODUCERS because only they carry out photosynthesis, and are capable of producing organic compounds (carbohydrates, which are then converted into fats and proteins), starting with simple compounds (CO2 and water), using the energy of sunlight, trapped by the pigment chlorophyll.
Animals are known as CONSUMERS because they eat and process these compounds, either incorporating them into their own bodies (assimilation) or using them (as "fuel") to provide energy (by respiration) for their activities.
Some animals (herbivores: "vegetarians") eat plants directly, and are known as PRIMARY (1st) CONSUMERS.
Other animals ("carnivores") which [usually kill and] feed on these animals are known as SECONDARY CONSUMERS.
Animals which feed on these are known as TERTIARY (3rd) CONSUMERS and QUATERNARY (4th) CONSUMERS. These are much rarer, because less and less energy is available as the transfer of organic material and energy is inefficient and much is lost in carrying out the ordinary processes of life.
Of course, both the organic materials in each of these animals, and the energy used by them, originate in the plant eaten by the primary consumer in the first place.
These descriptions are known as TROPHIC LEVELS.
This is the basis for food chains, food webs, and food pyramids.
(1) This is also the basis for the carbon and the nitrogen cycle, and other cycles in nature.
(2) Decomposers (mostly fungi and bacteria) also have a role in converting products (including dead remains) of plants and animals back into simpler substances which are re-used. Small creatures ("detritivores") feed on this waste ("detritus") and make this material available to decomposers and producers.
(3) In the deep oceans there are communities of organisms based around thermal vents where extremely hot water brings chemicals such as hydrogen sulphide up from the earth's interior. These food chains are not based on green plants carrying out photosynthesis (there is practically no light there). Instead, these food chains are based on bacteria and other micro-organusms which carry out chemosynthesis using the chemical energy from the water below.