Here is a wordy topic which also happens to be rich with physics and foundational in almost every aspect of engineering. The 2nd law of thermodynamics states that, you cannot build a device capable of extracting heat from something to do work without having some residual useless heat output. Perhaps more simply stated, you cannot convert a given amount of heat energy into exactly the same amount of work. There will always be some frictional type losses that reduce the total amount of energy available for work in any energy conversion process.
These frictional losses play a critical role in something known as entropy. Entropy is related to energy in a similar way that temperature is related to heat. Entropy is a measure of disorder or equivalently it is proportional to your lack of knowledge on the molecular details inside a system. In fact, the heat energy in a system is linearly related to both its temperature and entropy.
Another way to state the 2nd law of thermodynamics is to say that some useless heat byproduct will always occur in any physical process. As this useless heat is linearly proportional to the entropy, the entropy will always increase from any evolution of events. As the universe is continually undergoing various processes, the entropy of the universe is itself continually increasing.
This does not mean that local order cannot be increased due to work, it means that when local order is increased, the total disorder of all systems must overall increase. Creating carbon dioxide and water from combustion of fossil fuels is an example of converting energy into both work and waste heat associated with entropy and disorder.
The work done by running a diesel engine can increase order in noticeable ways such as moving materials, running a generator to do electrical work or operating some other piece of equipment. The initial order in the system present prior to burning the oil is found in having the diesel fuel molecules concentrated in one place and one configuration. When the fuel is burned to do some work, the organic molecules which made up the oil are all oxidized and converted primarily to CO2 and H2O which then diffuse into the air in a chaotic fashion.
When a drop of red die is kept separate from a glass of clear water, the system has a relatively high state of order. When the drop of die is placed into and mixed with the water, the disorder increases as the two are no longer distinct in the mix.
Even human work and effort increases entropy and disorder even when we locally decrease disorder by building and constructing ordered systems. The disorder from oxidizing food and converting it in a mix to waste products is always a process which overall increases total disorder.
The bottom line is that any useful work requires waste heat in some form or another. This observationally contradicts any perpetual motion machine. Even solar energy which is effectively a continual energy supply requires maintenance for solar panels and wind turbines due to various wear and tear on electrical and mechanical components as entropy marches on.