If you are comfortable with mountain driving, take I90 west to Buffalo then route 16 over the Bighorn Mountains to Worland then route 20 to Greybull and route 14 to Cody.
A longer route with no mountain driving is to stay on I90 to Laurel (just past Billings) then routes 212, 310, 72 and 120 to Cody.
We have done both in our Class C.
From Cody to West Yellowstone, the shortest way is to drive through the Park. If going direct from Rapid City to West Yellowstone, stay on I90 to Bozeman then route 191 to West Yellowstone.