Instructions
Defeat monsters. The simplest way to gain experience points (XP) in D&D is to kill whatever enemies or opponents the DM places in your path. Monsters typically have a set XP value, though the DM may adjust that value up or down depending on the character's strength or how difficult a time was had defeating the specific enemy.
Complete quests. Completing storylines often results in XP awards by the DM for a successful campaign. These XP awards are usually left to the DM's discretion, and may be greater or lesser depending on how easily you completed the campaign and how much you as a player participated.
Role-play. Some DMs award role-playing XP if you play your character particularly well. A dwarven cleric, for instance, forgoing the possession of a powerful magical artifiact because it conflicts with a particular belief he holds would be role-playing, and may prompt the DM to award bonus XP, especially since the role-playing resulted in the character taking a loss in terms of in-game possessions and power.
Solve puzzles. Again, these may be left up to the DM's discretion, but typically there is an XP award for solving a difficult riddle or puzzle. The exact amount of XP awarded can fluctuate depending on the level of difficulty involved in solving the puzzle as well as how long it took to solve.