Do you have to pay for a server to make a online multiplayer game? Or can I just code a server or something in the game?
A server in a multiplayer game is actual hardware running software and connecting the players. So yes, unless you find a free and most likely limited service, you will have to pay for a server.
You could potentially create a peer to peer multiplayer game (players connect to each other e.g. via WebRTC), but still you will need a server for the initial discovery and handshake between them.
do you know any servers that offer free hosting or something?
In my experience, companies offering free server hosting tend to offer extremely limited functionality and they try to upsell you every step of the way.
One possible starting option would be to find a cloud hosting provider offering a free trial. You could check out Google Cloud, they offer $300 in credits for new customers. The trial only lasts 90 days, then you’ll have to start paying.