From Pokémon Sun and Moon official site:
Use the Pokémon Refresh feature to care for your Pokémon. After a battle, Pokémon sometimes end up all dirty. When that happens, you can take care of them and get them all cleaned up. By caring for your Pokémon, you can also cure status conditions like poisoning and paralysis that were inflicted on your Pokémon in battle.
Pokémon that have grown very affectionate, thanks to petting them a lot and feeding them their beloved Poké Beans, will battle to the utmost for you. Sometimes they’ll avoid attacks from opposing Pokémon—and even hold out when they’re on the verge of fainting. Take good care of your Pokémon with Pokémon Refresh, and they’ll be great allies on your adventure!