Is energy the ability to do work? Here was my argument against this, the second law of thermodynamics, in effect says that, there is no heat engine that can take all of some energy that was transferred to it by heat and do work on some object. So, if we can not take a 100% the thermal energy of an object, and use it to do work, what about the thermal energy that is rejected to the environment, can we use all of that energy to do work on an object? No, if energy is supposed to be the ability to do work, well that’s a contradiction.
 A: "The ability to do work" is certainly a lousy definition of energy.
Is it "merely" a lousy definition, or is it actually an incorrect definition? I think it could be either, depending on precisely how the word "ability" is interpreted. But if the words are interpreted as they would be in everyday speech and everyday life, I would say it's an incorrect definition.
UPDATE -- What is a definition of energy that is not lousy?
This is a tricky issue. Defining a thing that exists in the real world (like you do in physics) is quite different than defining a concept within an axiomatic framework (like you do in math).
For example, how do you "define" Mount Everest? Well, you don't exactly define it, you merely describe it! You describe where it is, you describe what it looks like, you describe how tall it is, etc. Since there is only one mountain that has all these properties, you wind up with a "definition".
Likewise, if I start describing energy (i.e. listing out various properties of energy), I will eventually wind up with a definition of energy (because nothing except energy has all these properties). Here goes:


*

*The following are examples of energy: Kinetic energy, electric potential energy, gravitational potential energy, ...

*The fundamental laws of physics are the same at every moment in time -- they were the same yesterday as they are today. This fact implies, by Noether's theorem, that there is a conserved quantity in our universe... This quantity is energy.

*Special relativity relates energy to mass / inertia.

*General Relativity relates energy to the curvature of spacetime. 

*In quantum mechanics, the energy of a system is its eigenvalue with respect to the Hamiltonian operator.

*Whatever other things I'm forgetting or haven't learned...
All these properties are interrelated, and out of them bubbles a completely precise and unambiguous understanding of what energy is.
(I'm sure that some people will claim that one bullet point is the fundamental definition of energy, while the other bullet points are "merely" derived consequences. But you should know that this is a somewhat arbitrary decision. The same thing is true even in mathematics. What aspects of "differentiable manifold" are part of its definition, and what aspects are proven by theorems? Different textbooks will disagree.)
But can you boil that understanding of energy down into a one sentence "definition" that is technically correct and easy to understand? Well, I can't, and I doubt anyone on earth can.
A: *

*The 2nd Law, recasted (as you did) in terms of Carnot efficiency, just says the ideal scenario is all energy is converted to work while in reality there is a loss through some heating. So it doesn't contradict energy being the ability to do work.

*Your phrase "energy is the ability to do work" is justified by the Work-Energy theorem, i.e. $W=\triangle KE$. If you didn't start with kinetic energy, then use the Conservation of Energy Law first.
A: I've always liked and used Feynman's definition of energy as articulated in The Feynman Lectures (don't have the specific reference in front of me, but it's in volume one in the chapter on conservation of energy). Feynman defines energy as a number that doesn't change as Nature undergoes her processes. Of course, there are quite a few such numbers, but nevertheless energy is one of those numbers. You may also find the book Energy, the Subtle Concept: The discovery of Feynman's blocks from Leibniz to Einstein by Jennifer Coopersmith a useful reference.
A: Your statement of the Second Law is incorrect.  Your version should be "there is no heat engine that can take all of some energy that was transferred to it by heat and do work on some object in a cyclic process."  (My added words are in italics.)
It is certainly true that in a non-cyclic process all the heat can be converted to work.  Think of the expansion of a gas in a cylinder with a movable piston raising a weight.
As for the definition of energy, defining it as the capacity to do work seems to be as good a definition as one can easily get.
A: In my view, defining Energy as the capacity of performing work is a good definition, but it should be well understood. I will try to explain why in three steps.

*

*Since we say that energy represents a capability, it does not need to be necessarily actualized, i.e., be actually doing some work. This is especially important when one is considering potential energy.

A gas at high temperature has internal energy, but to be converted into work, one needs it to expand or to be connected to a cool reservoir by some heat engine.


*It is important to observe that this definition is implicitly referring to positive work. This is clear when we consider an elastic frontal collision between a mass m, with velocity v, and an identical mass m at rest.
The kinetic energy of the moving ball is converted into work, and, consequently, in kinetic energy of the second ball. In this situation, we have:
$v_{1,i}=v_0$,$v_{1,f}=0$,$v_{2,i}=0$,$v_{2,f}=v_0$.

The work that the first mass do in the second is given by $W_{1,2}=\frac{mv^2}{2}$.
The negative work that the ball at rest applies on the first ball, $W_{2,1}=-\frac{mv^2}{2}$, is basically due the action-reaction force pair.
Indeed, the kinect energy of the first ball can be exactly identified in this example with the work performed on the second ball. For different masses the kinetic energy is not fully converted in work, but it does not matter according to point 1.


*Such definition of energy should not be restricted to macroscopic work (also known as useful work or expansion work, in the case of gases). This can be verified by the comparison between "1 mol of gas at $300 K$ and 1 mol of gas at $500k$"   versus   "2 moles of gas at $400K$".

One might extract useful work from the first system by a heat machine and not from the other. However, both of them have the same internal energy. One might observe an apparent contradiction here.
Many other examples may be formulated to create an apparent contradiction between such definition of energy as the capability to perform work and the second law of thermodynamics.
The solution for such examples is that when some heat is liberated to the environment, the particles in the surroundings increase their average kinetic energy, and therefore some work was actually performed at the microscopic level.
--
That said, in my view, the capability of performing work is indeed a good definition of the quantity we refer to as 'Energy'.
In the Feynman Lectures, energy is defined as a quantity that is conserved in an isolated system. This is absolutely correct. However, I personally feel that this is too mathematically abstract and avoids the actual explanation of the "meaning" of such quantity that is conserved through all physical processes.
Finally, I would like to also suggest the reading of the brief paper by J.W. Warren (1982) for the European Journal Science Education: https://doi.org/10.1080/0140528820040308
