When and how are flagged questions and answers checked in relations to edits?
Depends on what kind of flag – and then how active the "workers" on the corresponding queue are. You wrote you flagged a question off-topic: if that was done via "vote-to-close" it ends up in the "community review queue", waiting for 4 more community members to agree – or disagree. Depending on that, when 5 close-votes (or less but a mod throwing the final one) have accumulated, the question is closed.
As you mention your flag having been "declined", that rather sounds like you've flagged it for mod attention. Then it was most likely declined as it (no longer) matched, as you assumed.
Are pending off-topic flags always declined when a question or answer is edited and are no longer valid?
Talking about mod flags: No, usually not. We (mods) might decide the flag is invalid – but we usually check if it was raised in good confidence; in which case we "dismiss" it as "helpful".
What can I do in the future to avoid flagging an off-topic question or answer that could potentially be declined?
If you indeed marked it for mod attention: better "vote to close" for being off-topic (no need to raise it for a mod, our community can deal with it fine – and we mods are participating in that as well).
If instead you raised a "regular flag" (i.e. used the "close" link – unlikely, as you'd need a little more rep for that), please clarify. I'm not aware of "declining" in that context – after all, those are not really "flags" but rather "votes" (hence "vote to close", not "flag to close"). Also note you can always retract such a vote.
That said, thanks for bringing it up – last but not least as a reminder for us mods to better look twice before deciding to "decline" a flag!