Then you would look at building a tree of all possible next moves from the current position. Then go to each of those positions and do it again.
Need to evaluate each position for win for either side and use that info to decide on your move.
I've forgotten the name of the technique. I think it was called alpha-beta when I used it years ago. I'm sure Google or wikipedia has a dozen modern techniques to use.

I wrote a simple TTT game in javascript that uses a table of winning moves that it searches for possible next moves. No AI, just pattern matching. TicTacToe using JavaScript