Controlling magnetism via electric fields addresses fundamental questions of magnetic phenomena and phase transitions1-3, and enables the development of electrically coupled spintronic devices, such as voltage-controlled magnetic memories with low operation energy4-6. Previous studies on dilute magnetic semiconductors such as (Ga,Mn)As and (In,Mn)Sb have demonstrated large modulations of the Curie temperatures and coercive fields by altering the magnetic anisotropy and exchange interaction2,4,7-9. Owing to their unique magnetic properties10-14, the recently reported two-dimensional magnets provide a new system for studying these features15-19. For instance, a bilayer of chromium triiodide (CrI3) behaves as a layered antiferromagnet with a magnetic field-driven metamagnetic transition15,16. Here, we demonstrate electrostatic gate control of magnetism in CrI3 bilayers, probed by magneto-optical Kerr effect (MOKE) microscopy. At fixed magnetic fields near the metamagnetic transition, we realize voltage-controlled switching between antiferromagnetic and ferromagnetic states. At zero magnetic field, we demonstrate a time-reversal pair of layered antiferromagnetic states that exhibit spin-layer locking, leading to a linear dependence of their MOKE signals on gate voltage with opposite slopes. Our results allow for the exploration of new magnetoelectric phenomena and van der Waals spintronics based on 2D materials.