The modern description of elementary particles, as formulated in the Standard Model of particle physics, is built on gauge theories. Gauge theories implement fundamental laws of physics by local symmetry constraints. For example, in quantum electrodynamics, Gausss law introduces an intrinsic local relation between charged matter and electromagnetic fields, which protects many salient physical properties including massless photons and a long-ranged Coulomb law. Solving gauge theories by classical computers is an extremely arduous task, which has stimulated a vigorous effort to simulate gauge-theory dynamics in microscopically engineered quantum devices. Previous achievements implemented density-dependent Peierls phases without defining a local symmetry, realized mappings onto effective models to integrate out either matter or electric fields, or were limited to very small systems. The essential gauge symmetry has not been observed experimentally. Here, we report the quantum simulation of an extended U(1) lattice gauge theory, and experimentally quantify the gauge invariance in a many-body system comprising matter and gauge fields. These are realized in defect-free arrays of bosonic atoms in an optical superlattice of 71 sites. We demonstrate full tunability of the model parameters and benchmark the matter--gauge interactions by sweeping across a quantum phase transition. Enabled by high-fidelity manipulation techniques, we measure the degree to which Gausss law is violated by extracting probabilities of locally gauge-invariant states from correlated atom occupations. Our work provides a way to explore gauge symmetry in the interplay of fundamental particles using controllable large-scale quantum simulators.