How those the first law of thermodynamics relate to the energy pyramid?
In an energy pyramid, all of the energy is transferred. It is not created or destroyed, just like the first law states.
The first law of thermodynamics states that energy cannot be created, nor destroyed; only transferred.
Now, use this visual diagram to help you understand the flow of energy in an ecosystem:
Note how all of the energy is somehow transferred. In the pyramid, the sun's energy is transferred from the primary producers all the way up to the apex predators at a factor of 10, with loss of heat in-between each level because the animals are hunting or being hunted.
When the animals die, their remains are broken apart by decomposers, and their energy goes back into the soil for new primary producers to use, along with the Sun's energy. The nitrogen cycle is also an example of this.
The Sun's energy comes from the combustion of Helium and Hydrogen, which has been going on for a long time. The exact origins of energy are unknown (Big Bang, et cetera..), but the fact still stands that we can't create or destroy energy.
https://en.wikipedia.org/wiki/Ecological_pyramid , Wikipedia Commons