Warning: DMG Spoilers Ahead. You have been warned.
It's difficult. I have one method, laid out below, but barring spells like Polymorph or other temporary attribute-boosting abilities, the most you'll get in a "normal" (read: 20th level) game is 24. There are magic items which can give you very high attributes (the legendary Belt of Storm Giant Strength, for example, will set your Strength to 29).
Here's one method of gaining 30 Constitution without resorting to temporary or shape-altering magical powers. There may be alternate, more efficient ways to do this.
Step 1: Be an elf (or a very young dwarf). You're going to need literal hundreds of years to pull this off, so this is important.
Step 2: Be a barbarian. This will shave 200 years off the time required.
Step 3: Reach 20th level. Along the way you need to get your Con score up to 20 with ASI; once you hit 20th level, you get another 4 points of Con. At this point, you should have a Con of 24. Note that this part (and the Barbarian part) can be ignored if you draw Star from a Deck of Many Things twice, though you still need to get your base score up to 20 through ASI before drawing Stars.
Step 4: Read a Manual of Gainful Exercise. This gives you 2 more points of Con, and raises your limit by an additional 2. You're now up to 26. Getting close! There are Manuals for every attribute, though some have different names; so if you want to do this for Strength through the same method, or some other attribute through the Star method, that also works.
Step 5: Wait 100 years, and read it again. This is where the "being an Elf or Dwarf" thing comes into play, as very few races can afford to sit on their arse for a century. The Manuals all include this line: "[After reading,] The manual then loses its magic, but regains it in a century." So, you wait for a century and read the manual again, bringing you to 28 Con. Rinse and repeat!
Congratulations! You now have a Con score of 30. It only took you 200 years.