What is the source of electricity in the electromagnets?

The source for an electromagnet can be any electrical supply.

Batteries or derived from the mains, using a transformed and rectified supply.