This work presents studies of the structure, dynamics and mechanical response of colloidal glasses and gels in the presence of applied stresses and aging. The dynamics and three-dimensional structures of the glass and gel are observed in real time by confocal microscopy and X-ray scattering. Recent development of X-ray scattering and confocal microscopy, and a new conceptual idea of a dynamical phase transition allow us to identify and investigate a new non-equilibrium first-order phase transition by connecting the non-affine motion of the particles to their free energy under deformation, and characterize the experimental trajectories of mobile and immobile colloidal particles in the presence of a shear field and aging.