I was reading a book about the sun which suggested that a solar flare can destroy every electronic item on Earth. Is this really true and if so, how would it happen? Could we do anything to to prevent it?
Can a solar flare destroy every electronic item on earth?
Let's start with an extreme upper limit, a supernova. So we know that a typical supernova will not eradicate our atmosphere, as I discussed in detail at: https://physics.stackexchange.com/a/233126/59023. It would also not affect most of our technology unless it was really close (i.e., the sun would need to collapse, but our current understanding is that the sun will not undergo a supernova explosion).
We also know that due to the properties of most power grids, the likelihood of numerous transformers suffering irreparable damage is quite low as well, as I discussed in detail at: https://physics.stackexchange.com/a/149199/59023. The reason is that if one major transformer were to fail, the rest of the grid would "trip" causing a temporary wide spread blackout. The grid could, in principle, recover shortly afterwards work just fine. Thus, so long as the duration of the large and rapid changes in the Earth's magnetic field from external influences (e.g., coronal mass ejections or CMEs) have subsided, the rest of the grid would be fine. We have several groups actively studying the effects of the sun on Earth, as I discussed at: https://physics.stackexchange.com/a/214509/59023.
Finally, contrary to what Hollywood might want you to believe, solar storms are not going to kill us (well, at least not directly). I wrote a detailed answer at https://physics.stackexchange.com/a/258093/59023 that basically explains why the combination of Earth's atmosphere and magnetic field shield us from most solar threats. Spacecraft and astronauts, however, could be in harms way during very strong storms.
Is this really true and if so, how would it happen?
Of course, it is possible to damage nearly all electrical devices with an EMP, but that does not mean the sun is capable of such a feat nor that it is likely to happen. If the power grid were to catastrophically fail (i.e., dozens of major transformer failures), many electronic devices would still be fine and useable. It's just that we would all have to buy small generators to use them because the electricity in your home wouldn't be on otherwise.
Could we do anything to to prevent it?
Yes, it's rather simple. If we know that a large geomagnetic storm were about to occur, we could simply power down the grid. The damage to transformers arises because the ground induced currents produce a DC offset in the voltage/current arriving at the transformer. They are designed to convert power for a given frequency and amplitude. Thus, if you shift a sine wave away from zero, the peaks will appear to the transformer as larger amplitude signals. These will be clipped initially but they create excess heat and internal stresses on the transformer. Over time, the heat and stress cause the transformer to effectively tear it self apart (Note if you ever get to hear one explode, it's one of the loudest things you will likely hear.).
If there is no power in the system, then the DC offset will have no real effect for numerous reasons. One could, in principle, also employ high pass filters but I am guessing cost and maintenance would be limiting factors.
The main vulnerability is our spacecraft in orbit and it's not the induced electromagnetic fields that will cause them issues. Their biggest threat is high energy charged particles, which often come with large solar storms. The details can be found in my answer at https://physics.stackexchange.com/a/258093/59023.
Here's the how: very energetic charged particles interact with the Earth's magnetic field, and when they do so they emit electromagnetic radiation. If the energy of this radiation was high enough then when it reached wires/conductors at the Earth's surface the opposite effect would take place: ie the EM radiation would result in the flow of charged particles (a current). Such currents, if strong enough, could damage very many electronic and electric devices.
As to the possibility: it seems unlikely that such a high energy event would take place (ie to destroy all/nearly all electronic equipment), but it could be possible. We don't have sufficient data.