Can you use an Attack action (using two hands) then also be able to cast a spell as either a bonus or reaction using a free hand after the fact?
Conversely, can you cast a spell with a free hand and then also get your reaction attack of opportunity using your two hands for that?

Yes to using an Attack action and then cast a bonus action or reaction spell using your free hand (the one that was just holding the weapon but is now casting the spell).

Yes to the second, provided that you still have your Reaction available. You cast a spell on your turn, then it’s enemy’s turn, enemy moves away from your reach, you get an opportunity attack if you want to use your Reaction for that.

According to this ruling, you can take a hand off your greataxe to cast a spell as a Bonus Action, grip it again and swing it as a Normal Action, take a hand off again to open a door, hold it again before you end your turn because you expect to do an Opportunity Attack later, and then during somebody else’s turn take a hand off again to cast Shield as a Reaction.