Studies of ultracold atoms in optical lattices link various disciplines, providing a playground where fundamental quantum many-body concepts, formulated in condensed-matter physics, can be tested in much better controllable atomic systems, e.g., strongly correlated phases, quantum information processing. Standard methods to measure quantum properties of Bose-Einstein condensates (BECs) are based on matter-wave interference between atoms released from traps which destroys the system. Here we propose a nondestructive method based on optical measurements, and prove that atomic statistics can be mapped on transmission spectra of a high-Q cavity. This can be extremely useful for studying phase transitions between Mott insulator and superfluid states, since various phases show qualitatively distinct light scattering. Joining the paradigms of cavity quantum electrodynamics (QED) and ultracold gases will enable conceptually new investigations of both light and matter at ultimate quantum levels, which only recently became experimentally possible. Here we predict effects accessible in such novel setups.