Purchases are tied to both the system and your NNID/Nintendo account on Wii U, and your NNID can only be used on 1 Wii U system.
It is possible to get your NNID (and digital purchases) un-linked from a Wii U you no longer own to a new one, but you have to contact Nintendo and it's not guaranteed. Before you sell, it might be worth noting down the serial number if you're planing to buy another Wii U in the future.
After formatting the system, I think you will still be able to downloaded your purchases via the eShop without signing in with your NNID again, so you can still advertise/sell the digital games with the system if you never plan to get them back.