I wanted to play Minecraft with a friend the other day, but then it occurred to me that I didn't even know how to do it. I've read that you need some kind of code to do this, but I don't know where to get it.
Do you play Java or bedrock edition?
In Minecraft you have to rent a server for this.
You can do that here, for example:
https://www.g-portal.com/
https://server.nitrado.net/deu/gameserver-mieten
Java or Bedrock Edition?
Java
Java Edition
If you are in the same Wi-Fi network, one of you can start a single player world and open it in the pause menu in the LAN. Then the other can enter this world via multiplayer.
If you are not in the same Wi-Fi network, you need a server or a realm. Some servers are free, but then the server's performance won't be as good. You can rent a good server for a fee or create one for free on ploudos.com or aternos.org.