The Hubbard model underlies our understanding of strongly correlated materials. While its standard form only comprises interaction between particles at the same lattice site, its extension to encompass long-range interaction, which activates terms acting between different sites, is predicted to profoundly alter the quantum behavior of the system. We realize the extended Bose-Hubbard model for an ultracold gas of strongly magnetic erbium atoms in a three-dimensional optical lattice. Controlling the orientation of the atomic dipoles, we reveal the anisotropic character of the onsite interaction and hopping dynamics, and their influence on the superfluid-to-Mott insulator quantum phase transition. Moreover, we observe nearest-neighbor interaction, which is a genuine consequence of the long-range nature of dipolar interactions. Our results lay the groundwork for future studies of novel exotic many-body quantum phases.