Wild Pokemon can only escape if they have run away, use Teleport, or you or they use Roar or Whirlwind.
Roaming Legendaries can escape by themselves all of the time, unless you trap them with a partial trapping move (Ex: Fire Spin), or use Mean Look on them. If your Pokemon has Shadow Tag, then they will also not be able to escape. If they have Roar, however, they will still escape.
Please feel free to comment any mistakes, I will make the edits when I can.