AI in its simplest form, and generally what we think of when we say AI in regards to games, is a state machine. In short "in this situation" "do this". So take the example you used. He moves towards the player, then attacks. So you have one event (or several, depending) that checks if the enemy is close enough to attack. If he isn't you move him closer. When he's close enough, you have events that makes him attack. That's about as basic as it gets, but even the smartest game AI follow similar foundations.