Playing Pokémon by yourself is fun, but you can't really say you've properly played Pokémon until you've played with others! That said, if that's not possible (because you live somewhere remote or you're a bit of a social recluse; we're not going to judge), feel free to skip this section--or keep reading, while dreaming of the possibilities.
Near or Far?
It doesn't matter if you want to trade or battle, the first thing you must do is open up the Y-Comm, by pressing the Y Button (hence the name) while on the field. This will be available after reaching Professor Magnolia's abode on Route 2, roughly one hour into the game.
By default, your game will be communicating via Local Wireless. If the person you're trading with is somewhere close to you, this is what you want. However, if the person is faraway and both of your Nintendo Switch consoles are connected to the Internet, press the "+ or - Button" to connect to the Internet.
To connect online, you must have a paid Nintendo Switch Online (NSO) subscription. If you don't have a subscription, the game will bring up a window informing you about NSO. From here, you can directly access the Nintendo eShop to purchase or renew your subscription.
Initiating a Request
Next, if you want to trade, press the Link Trade button along the top of the Y-Comm menu. Choose "Start trading" if you want to trade with a random person. If you're trading with a nearby person and you don't believe there are other players around, you might be safe choosing this. Otherwise, pick "Set Link Code" to enter a private 4-digit code.
Or if you want to battle, press the Link Battle button near the middle. You can choose from three battle formats: Single Battle (1 Pokémon versus 1 Pokémon, like most battles in the story), Double Battle (2 Pokémon versus 2 Pokémon) or Multi Battle (a Double Battle between 2 teams of 2 players). Similarly, you can set a link code by pressing the + or - Button.
After this, you wait. Your trade or battle request will be issued as a stamp and, eventually, it should appear on the Y-Comm menu for other players. Other people can then click your stamp to accept the request. The reverse is also true--you can try to accept a request by selecting another person's stamp. To speed things up, you can click "Search" to filter the stamps.
If a link code has been set, after selecting the stamp, the other person must input the correct code for the request to go through. There's also a quicker and slightly less fussier way to arrange a trade or battle. When you and another player both set a link code and you both choose the same link code, the game will try to pair you two together.
Limitations of Link Codes
When setting a link code, if you're aiming to play with a particular person, try to choose numbers that are hard to think of. Don't pick something like "1111" or "1234". Because with potentially thousands of people playing the game, there's a very high chance that other people may be using the same link codes.
With luck, your trade or battle request will be accepted. From here, everything should be self-explanatory. When trading Pokémon, you both have to wait for each other to respond. After the other person offers their Pokémon, you can agree to trade, check the summary of the Pokémon being offered, your Pokémon's summary or cancel that particular request.
For battles, the game will randomly let you or the other player(s) decide the rules. This will determine things like the Level of your Pokémon and whether you can use certain "special" Pokémon or not (powerful Legendary Pokémon such as Zacian, Zamazenta and Eternatus and Mythicals like Mew). Afterwards, you can select your team, as well as the battle music.