To fix the failed to connect error in Valheim, you will first need to make sure both your server and game are fully updated.
Chances are, what is causing it is that you or the server has an outdated version of the game. If that doesn't help, try verifying the files by going into the Steam library. In Steam Library right-click on Valheim and click properties as shown below.
Doing so will cause the properties tab for Valheim to appear as pictured below. When this tab pops up, click local files. Under local files, you can verify the files for Valheim by clicking verify the integrity of game files as shown below.
Clicking verify the integrity of game files will have Steam run a check to see if you are missing any game files. So press start and let Steam do its job. If you are missing anything, Steam will automatically download the missing files.
After the download is finished, retry joining the server. With luck, your failed-to-connect error will go the way of the dinosaurs. However, if one asteroid wasn't enough, you can always try updating your server by playing around with SteamCMD.
How to update your Valheim server using SteamCMD
Install SteamCMD and create a .txt file in the Valheim source files. Label this .txt file Update and change the file to a .bat. Next, add your SteamCMD folder location followed by login anonymous, force_install_dir, then the location of the Valheim folder, app_update 896660 validate.
Save the text and then double the file to run it and update your server, and once it's updated, fire it up and see what happens. You can also try adding a launch option of +connect your server IP and port to the game.
To do this, right-click Valheim, click properties, and find launch options at the bottom of the general tab. Paste +connect and then add your server IP followed by your server port.
For more awesome Valheim guides, check out our entire Valheim catalog.