There are a few ways to do this. Note that all of the following methods require software modification unsupported by Nintendo that have the possibility of damaging your console and voiding your warranty - they must be done at your own risk.
The most popular replacement online server for Nintendo WiFi Connection is Wiimmfi, which has about 300 players online at any given time. In order to access this server, games must be patched in order to connect to this separate server. If you had a friend code from Nintendo WiFi Connection, a new code will automatically be generated for you when you first connect to Wiimmfi. In addition, from the first time you connect to Wiimmfi in Mario Kart Wii you must wait 7 days before being allowed to play online (in Mario Kart Wii only, all other games let you play immediately). This is to deter hackers from playing.
There are several methods you can use to connect to Wiimmfi.
Method 1: Custom DNS and View EULA (easiest, Wii ONLY, no custom tracks)
Requires: Nintendo Wii with Internet Connection
All you need to do is set your Wii internet connection's primary DNS to 184.108.40.206 (and secondary DNS to any other valid DNS server, such as 220.127.116.11) in your Wii's network settings. Then, any time you want to play online, all you need to do is go into the Wii's network settings and view the EULA/User Agreement. A video tutorial is available for this method.
Method 2: Internet Channel (easy, Wii ONLY, no custom tracks)
Requires: Nintendo Wii with Internet Connection, Internet Channel installed
You can use the Internet Channel to launch a Wiimmfi patcher by navigating to http://chadsoft.co.uk/wiimmfi/ and adding this page to your bookmarks. Each time you want to be able to connect to Wiimmfi, you must run this exploit by using the bookmark in the Internet Channel (it must be run by clicking on the bookmark).
I strongly suggest that you watch this video for the full directions.
Method 3: CTGP-R [Custom Track Grand Prix - Revolution] (intermediate , Wii and Wii U, custom tracks)
Requires: Nintendo Wii or Wii U with internet connection, SD card, computer or Android phone capable of writing to SD cards, unzipping files, connecting to the Internet
CTGP-R is a mod for Mario Kart Wii that will also let you play online, with enhancements such as VR matching and cheat prevention.
In addition to the 32 original tracks, it adds 216 custom tracks to the game, an item rain mode (items fall from the sky), a countdown mode (hit others with points to get more time to race, farthest ahead wins), a new time trial ghost server with cheat detection, a cup organizer, an option to store the game’s save file on the SD card, regular automatic updates that change custom tracks and add new features, automatic friend code update for Wiimmfi, as well as other features. Unfortunately, you cannot play battle mode while running this mod with custom tracks enabled within the launcher settings.
CTGP-R is harder to install, but works on both Wii and Wii U.
Methods vary by system and software version.
This video is a tutorial for installing CTGP on Wii version 4.3.
This installation guide has written instructions for all system versions of Wii and Wii U.
It is also possible to play competitions/tournaments on Wiimmfi, but that requires a separate patcher. Information can be found on the Wiimmfi website.
Any error codes you get from Wiimmfi can be looked up here: https://wiimmfi.de/error.
Remember that using Wiimmfi requires software modification that could void your warranty, so do it at your own risk.