If you lose, you will 'black out' (retreat) to the nearest Pokémon Center where all your Pokémon will be fully healed. If you 'black out', whether from a Trainer battle.
When a Pokémon has been poisoned, it will lose HP during battle, which can cause it to faint. Additionally, prior to Generation V, a poisoned Pokémon will lose 1.
Trainers from opposing teams battle to reduce the motivation of the Pokémon on the Gym. Pokémon gradually lose motivation over time and by losing battles.
But what happens if you do try to fight it and you lose? I will usually only fight slightly higher Pokemon when I've got a ton of Potions or Revive elixirs, because.