We study the atom-light interaction in the fully quantum regime, with the focus on off-resonant light scattering into a cavity from ultracold atoms trapped in an optical lattice. The detection of photons allows the quantum nondemolition (QND) measurement of quantum correlations of the atomic ensemble, distinguishing between different quantum states. We analyse the entanglement between light and matter and show how it can be exploited for realising multimode macroscopic quantum superpositions, such as Schrödinger cat states, for both bosons and fermions. We provide examples utilising different measurement schemes and study their robustness to decoherence. Finally, we address the regime where the optical lattice potential is a quantum dynamical variable and is modified by the atomic state, leading to novel quantum phases and significantly altering the phase diagram of the atomic system.