mostly this is an experience thing. the longer you board the less it'll happen just be confident with your movements is the only true advice I can give you.
It seems like it happens most often when I'm turning toeside or heelside and my back foot goes out too far backward/forward and then I can't get out of it, so I just put my board flat. I guess I shouldn't do that. How do you get out of that situation?