Driven-dissipative dynamics of atomic ensembles in a resonant cavity: Quasiperiodic route to chaos and chaotic synchronization