As I know, the engine is independent from the electrical system, and it is supplied from magnetos. However, how can a pilot deal with a magneto failure? And also, when one magneto fails what problems can occur?
One of the things the checklist will have you do prior to lining up on the runway in a piston engine aircraft is a magneto check. That serves to verify that both magnetos are in good working order and that the grounding of both magnetos is intact.
Typically, that will involve bringing the engine to some given power setting (most likely expressed in terms of RPM, but I suppose it could be measured in some other way for some engines; manifold pressure, maybe?) while holding position, then turn one magneto off, then back on, then turn the other off, then back on. While doing so, you verify that there is a drop in engine power, and that the drop in power is symmetric (about the same for both magnetos), and of course that the engine keeps running on either magneto alone.
This verifies that both ignition systems (magneto, spark plug, etc.) are working, and that the grounding of the magnetos works as expected. Absent the power drop, you could have a hot magneto, which isn't a huge problem in flight but could be a major headache on the ground.
If a magneto fails in flight, especially in a single you might not even notice initially; there will be a slight reduction of engine power, just like during the magneto check, but the engine will keep humming along on the remaining magneto. That's part of the purpose of having two independent ignition systems: redundancy.
If there is an engine failure in flight, the checklist will likely dictate something like "select best magneto" at one point or another during the restart sequence. Basically, you'd be doing a magneto check in the air, and choosing whichever setting gives you more power. If one magneto has failed outright, this will very likely cause the engine to stop when you select only the bad magneto; you'd then have to select the other (or both) and restart the engine again.
The checklist may or may not specify that you should land as soon as possible after a magneto failure, but if it were me, I'd want to land regardless and have the problem checked out properly. That would mean either just landing and taxiing back to the ramp (if, say, you're in the pattern doing circuits), or diverting and landing (if you're on your way to somewhere). There's always the possibility that whatever caused the first magneto to fail might cause the second to fail as well, and I'd rather be on the ground at the wrong airport than in the air with a non-functional engine.
Generally when you have a mag malfunction you want it turned off. The mag may just quit entirely, but there are other failure modes, such as loss of spark to one or more plugs, or worse, a shift in the mag's internal timing, and either of those can cause rough running and potentially detonation (where something causes the timing to drift in the advance direction - the mag's timing is fixed and is set mainly by its orientation on its mounting pad on the engine, but there is an internal timing process for the breaker points when the mag is set up on the bench, called setting the P-gap).
Because of this, one of the normal things to do with a rough running engine is to switch to each mag and see if the roughness stops.
The main effect of having a mag switched off is the obvious one, a loss of redundancy, and a small power reduction. Aircraft cylinders being quite large, the flame has a long way to go to the other side of the combustion chamber when the plug fires. Dual plugs ignites the mixture on opposite sides of the cylinder at the same time, and the flame just has to travel from each side to meet in the middle. One a single plug, the flame travels from the single ignition point across to the other side and this takes twice as long. The effect is more or less similar to retarding the timing a bit; delayed build-up of combustion pressure and less complete combustion, so there is a reduction in horsepower of a few percent.
Also note that if a mag completely stops working while you're in cruise, without any intermediate roughness, you probably won't notice it (unless you were intently starting at the tach or carefully focused in on the sound) and will be unaware of it until the next runup on the ground. You might notice the change if it happens at take off power, on a fixed pitch propeller, from the RPM drop, but if it's a constant speed prop, you won't detect the change as an RPM change and you may or may not notice the subtle change in sound as engine torque declines slightly.
A dormant failure of a single mag that lasts for one flight is an insignificant risk, so it's not like you need to continuously check the mags any more often than once per flight (and even that's overkill; when I was bush flying we only did a full run-up first flight of the day, although on subsequent flights I would usually quickly flip between the two while taxing to the end of the lake for takeoff, just to confirm they were both working).