Solar and stellar magnetic fields and atmospheric structures: Theory