Flashing around chimney and gap
You should caulk this with the best exterior caulk you can buy (doesn't have to be roofing, but do NOT use latex caulk for this). Don't use expanding foam, as it's not waterproof.

You might want to double check and make sure there's no lasting water damage (i.e. wood rot) before you do.