[verb て-form] + よかった [verb 〜ない → 〜なくて] + よかった [i-adj 〜い → 〜くて] + よかった [na-adj] + で + よかった [noun] + で + よかった
てよかった means "I'm glad": something turned out well and you're relieved or pleased about it. It goes on the て-form of the verb, and よかった is the past of いい. Add です in polite speech, or ね to share the feeling with the listener.
The verb stays in the て-form, never the た-form, even though the event is over: ×日本に来たよかった. And よかった stays in the past where English says "I'm glad".
For relief that something did not happen, the verb goes into the negative て-form, 〜なくて.
An adjective or a noun works too: an い-adjective becomes 〜くて, a な-adjective or a noun takes で.
They point in opposite directions. てよかった is glad about what did happen. ばよかった wishes something had happened that didn't: it is regret. "I wish I had…" always takes ばよかった.