The both are OK and it is difficult to come up with a case where they are not interchangeable. I will try.
Во сне он был молодой = He looked young while sleeping.
Во сне он был молодым = He was young in his sleeping dream.
Suppose there is a fairy tale where a magic ring makes the person young temporarily.
Когда я был молодым = When he was young due to the ring, during the
duration of spell. Can also mean when he was truly young.
Когда я был молодой = When he was truly young, cannot mean young due to
the magic ring.
So, "Когда я был молодой" means when I was truly young, "Когда я был молодым" can mean besides truly young also young due to some factors like imagining oneself young or be temporarily young or playing a role of a young.
Когда я слышу эту песню, я становлюсь молодым = When I hear this song,
I feel as a young, much better, stronger than usually. Physically change to a young man. Can mean he can date young women.
Когда я слышу эту песню, я становлюсь молодой = When I hear this song,
I re-asses my current age, despite my actual age, I think of it as of
quite young age. Can mean he can date women of his age as if the both were young.
Also, молодой can mean a bride or any other slang or special meaning (like young role in a play etc), so if you mean молодой as a noun, use
Когда я был молодым