Is Joule's law of heat generated by electricity only applicable for ideal resistors? I have learned about Joule's law of heat generated by electricity. It states that $H=VIt=I^2Rt$. But here's where I'm confused: if the whole electrical energy does not get converted into thermal energy then how can we write $H=W=VIt=I^2Rt$? Does it mean that Joule's law is only applicable for ideal resistors? As a student, I may have made some mistakes. If it occurs, give me the proper knowledge about that please.
 A: 
But here's where I'm confused: if the whole electrical energy does not
get converted into thermal energy then how can we write
$H=W=VIt=I^2Rt$?

The complete conversion of electrical energy into thermal energy applies to the resistance portion of the circuit. Joule heating has a coefficient of performance of 1, meaning 100% efficient in converting electrical energy into thermal energy in resistance.
The voltage $V$ across the resistor equals the work $W$  per unit charge $Q$ to move the charge through the resistor, or
$$V=\frac{W}{Q}$$
Current is the rate of charge of charge transport through the resistor or
$$I=\frac{Q}{t}$$
The product of the voltage across the resistor and current through the resistor is the rate of electrical work (electrical power $P$) done in moving the charge through the resistor
$$P=\frac{W}{Q}\frac{Q}{T}=\frac{W}{t}$$
Then the total energy $E$ delivered to the resistor in time $t$ is
$$E=Pt=W=VIt$$
Then finally, from Ohms law, $V=IR$
$$E=I^{2}Rt=\frac{V^{2}t}{R}$$
All of this energy is converted into internal energy of the resistor due to collisions between the charge carriers (typically electrons) and the particles of the resistor (usually atomic ions), raising the temperature of the resistor. When the temperature of the resistor is greater than the temperature of its environment, there is heat transfer to its environment.
A rough mechanical analogy is the mechanical work moving an object at constant velocity on a surface with friction. All the work in moving the object between two points is converted to heat due to kinetic friction.
Hope this helps.
A: (a) I think most physicists would interpret an ideal resistor as a resistor that obeyed Ohm's law (that is $I$ proportional to $V$). It is not necessary for a resistor to be ideal in this sense for your formula and its interpretation as thermal energy generated, to hold good, provided that $R$ is taken as the ratio $V/I$, whether or not that ratio is constant.
(b) You seem to be considering cases in which there are non-resistive elements, such as capacitors. Let's consider a 'component' in the form of a black box containing a capacitor and a resistor (Ohmic or otherwise) in parallel. If we apply an increasing pd $V(t)$ across the terminals of the black box, then $V(t)I(t) dt$ doesn't give the thermal energy evolved in time $dt$ if $I$ is the current going into (and coming out of) the black box. That's because some of this current is charging the capacitor – a non-dissipative process. For your formula to give thermal energy evolved, $I$ must be just the current through the resistor.
You should consider for yourself the case of a capacitor and resistor in series, heeding Chet Miller's comment.
A: In the case of a DC fan, while the contacts supply a DC voltage, a commutator changes several times per second its polarity in the electromagnets of the motor, in order to keep a torque between stator and rotor. So the voltage (and current) in the device is far from continuous, and its relation is not given simply by Ohm's law, but by $$V = L\frac{dI}{dt} + RI$$
The first term of the RHS is responsible for the mechanical energy: $w = \int T.d\theta$, where $T$ is the torque from the magnetic force and $\theta$ is angular displacement.
The power for a DC motor is $$VI = LI\frac{dI}{dt} + RI^2$$
It is designed so that the first term of the RHS is much bigger than the second one. The second one is responsible for the heat loss.
The mechanical analog is to push and pull an object inside a fluid (air for example). If it has a big mass (that corresponds to the big inductance of a motor), most of the work is done to overcome the mass inertia, changing its kinetic energy, and less from the fluid drag (what corresponds to the electrical resistance).
