The easiest way is to record a play through of the game using some kind of screen-recording software and just play that back. It's probably also the least flexible way, but it would work.
A more interesting solution is to build a record-and-playback feature into your game. These are also known as "replay systems," and you can find quite a few questions about them on here if you search around (try game-recording).
The basic concept is to first record all the input (along with timestamps of that input) as it happens in your game, and save this recording out into a buffer somewhere you can load back up.
Then you load it back up and run the game in a mode where instead of processing input from the actual controllers or keyboards or whatever, you pop an input event off the recorded queue, wait until the appropriate relative amount of time has passed since playback began, and then feed that input to the rest of your game as if the user had simply pressed the correct button.
This often requires you to have a deterministic simulation, so you will find that you'll want to save the random seed used to initialize any random generators with the recording buffer as well, and make sure that everything in your gameplay (that matters) is keyed to that seed so you don't get out-of-sync errors.
Then it's simply a matter of recording a good-enough demo run yourself, saving that buffer as an asset with your game, and then in the "title screen" game state, playing that recording back (probably on a loop of some sort).