There is not a lot you can do in py2 to stop boarders. You can have good defending crew, but you have to rely on them to be the crew that goes to repair a room damaged by boarders. Make your critical attack crew be primary repair on reactors, since that’s the most common boarding target. Also, if you are getting boarded in another room like shield or teleport, station a critical defender or freeze crew there. The more crew you have with critical strike or poison gas the better, since they’ll automatically use their ability when an opponent’s crew passes through their room, if you have “none, use ability”
Hunt and seek (Py3)
The most useful command in py3 is “friendly room has enemy”. With this, it’s super easy to program your defending crew to find and attack the opponent’s boarders. Have your defending crew close to where you think enemy crew will port to, but not so close that they’ll be caught in explosions from your mines. Note that only crew with defending abilities should be given this AI. If you have all crew try to attack boarders, the most senior will go and that’s probably not the best crew to defend with.
All crew entering a room will use their ability BEFORE a crew that is stationary in that room, whether they are on their own ship or the opponent’s ship. This means that defending crew can get off their critical attack ability as they enter a room that has boarders in it before they are attacked. Just remember that crew who teleport in have a short delay before they can use an ability; this is an exception to the rule that crew entering a room use their ability first.
1) Ability command (critical strike, freeze, or poison gas)
2) Friendly room has enemy, target condition room
3) Repair AI
Further reading for advanced captains
Amino^ has written a detailed counter-boarding guide that is worthwhile to read if you struggle with teleport rush. It is aimed primarily at end-game ships.