You get Solgaleo in Pokémon Sun, and Lunala in Pokémon Moon. This event happens after you complete the games' final trial.;
, If it's your first time doing so, you'll be treated to a long cutscene that includes a fight with Tapu Koko, where you can catch it, before the credits.
, If you're playing Pokémon Moon, go to the Altar of the Munne during the daytime. Either way, once you're there, go through the rift to appear at either the Altar of the Munne in Pokémon Sun or the Altar of the Sunne in Pokémon Moon. The fastest way to make it there is to use the Charizard Glide.
 In Pokémon Moon, go to the Lake of the Munne. Either way, once you're there, you'll see a cutscene where a Cosmog appears. After the cutscene ends, go up to the Cosmog and press A to get your very own Cosmog.
Catch Solgaleo or Lunala, depending on whether you have Pokémon Sun or Pokémon Moon.
Defeat the Elite Four and Champion.
Put Solgaleo or Lunala in your party, if you don't already have it in your party.

,
If you're playing Pokémon Sun, go to the Altar of the Sunne during the nighttime.
In Pokémon Sun, go to the Lake of the Sunne.
{"src": "wikihow-all_validation", "title": "How to Get Cosmog in Pok\u00e9mon Sun and Moon"}