Generalized Symmetries and Defects in QFT and Gravity