What are some stronger moves I can play next time in the King's Indian Defense? [fen ""]

1. d4 Nf6
2. c4 g6
3. Nc3 Bg7
4. e4 d6
5. Nf3 O-O
6. Be2 e5
7. O-O Nc6
8. d5 Ne7
9. Ne1 Nd7
10. Be3 f5
11. f3 f4
12. Bf2 g5
13. b4 Nf6
14. c5 Ng6
15. cxd6 cxd6
16. Rc1 Rf7
17. Nb5 a6
18. Nc3 Bf8 
19. Na4 Bd7
20. Nb6 g4
21. Nxa8 g3
22. hxg3 fxg3
23. Bxg3 Nh5
24. Bf2 Ngf4
25. Nb6 Rg7
26. Nxd7 Qxd7
27. Kh2 Nxg2
28. Nxg2 Rxg2+
29. Kxg2 Nf4+
30. Kg1 Qh3 0-1


I won that game, but I feel like I got lucky. I blundered in this game. I feel that letting white capture my rook on a8 was a blunder. Also what if white captured g4 then I have to capture e4 with my knight. White then captures my rook at a8. I don't see any compensation for black for the lost of material. I also don't see how to continue the attack through this variation. What could I have done better as black against my opponent? What are some key ideas in the King's Indian Defense that I missed in this game? What concepts should I grasp from this game I played to become a better chess player and better using the King's Indian Defense? When is it the right time to give up the rook on a8 for an attack on the king side?
won that game, but I feel like I got lucky. I blundered in this game. I feel that letting white capture my rook on a8 was a blunder


Everything you said was correct. 


  Also what if white captured g4 then I have to capture e4 with my knight. White then captures my rook at a8. I don't see any compensation for black for the lost of material. 


Again, I agree with your conclusion.


  I also don't see how to continue the attack through this variation.


You can't, because White plays Rc7 and keeps everything under control. Even if you "get" classical ...Bh3 sacrifice he will have Rxc7+ in that setup and your attack is dead. Bishops and knight will easily repel your minor pieces, not to mention that extra rook...
Since it was long time since I played actively KID ( both as Black and White ), I had to check theory as well. You missed the key move 19...Nd7! which stops White queenside action in its tracks, in my opinion. This is also the move theory recommends.     


  What could I have done better as black against my opponent?


I think your first mistake, and very serious one, was 19...Bd7?!. I would simply follow theory and respond with 19...Nd7!, stopping White's shenanigans at queenside. His attack is severely slowed down, while you can continue the attack with ...h5. This is the only important moment you can use in this game to improve.

Aside from that, you made some serious blunders he could have exploited. After he took the rook the game was over, but you managed to "come back". In many occasions you gave him a chance for turnover, which is a result of poor combinational skill. You need to practice more, especially when you play this opening, and especially when you play this line. Tactics is everything for Black here! See the comments in the game below to see all the mistakes and suggested improvements.

[Title "Game analysis"]
[fen ""]

1.d4 Nf6 2.c4 g6 3.Nc3 Bg7 4.e4 d6 5.Nf3 O-O 6.Be2 e5 7.O-O Nc6 8.d5 Ne7 9.Ne1 Nd7 10.Be3 f5 11.f3 f4 12.Bf2 g5 13.b4 Nf6 14.c5 Ng6 15.cxd6 cxd6 16.Rc1 Rf7 17.Nb5?! ( 17.a4!? ) 17...a6 18.Nc3 Bf8 19.Na4 Bd7?! ( 19...Nd7!? ) 20.Nb6 g4? ( 20...Rb8!?+/= ) 21.Nxa8+- g3? ( 21...Qxa8 22.Rc7+- ) 22.hxg3 ( 22.Nc7!? gxf2+ 23.Rxf2) 22...fxg3 23.Bxg3 Nh5 24.Bf2 ( 24.Bh2!? Bh6 25.f4 Nhxf4 26.Bxf4+- ) 24...Ngf4 25.Nb6 Rg7 26.Nxd7?? ( 26.g4!+- Nh3+ 27.Kh1 Nxf2+ 28.Rxf2 Qxb6 29.Rh2 ) 26...Qxd7?? ( 26...Rxg2+! $8 27.Nxg2 Qg5-+ ) 27.Kh2?? ( 27.g4!+- ) 27...Nxg2?? ( 27...Rxg2!+ $8 28.Nxg2 Qh3+ 29.Kg1 Qxg2# ) 28.Nxg2?? ( 28.Rc7!! Qc7 29.Nxg2+- ) 28...Rxg2+ 29.Kxg2 Nf4+ 30.Kg1 Qh3 0-1



  What are some key ideas in the King's Indian Defense that I missed in this game?


You missed nothing, push ...f5 -> deliver checkmate or die. The concept is easy to grasp :)

The only time you sidestep this pattern is in Yugoslav attack, but that is a different story ( White there goes for fianchetto with g3 + Bg2 ). In that case you prepare exchange sacrifice with ...Rxb2!, but this is off-topic here. If you need further questions about this line post them as a new question.


  What concepts should I grasp from this game I played to become a better chess player and better using the King's Indian Defense?


You can't play this line unprepared, you must know theory of this line very well. 

This line is so forced that one wrong move will cost you the game. Already after 19...Bd7?! White stood better! You had no counterplay after that, and if you played ...Rb8 he could simply take your light-squared bishop and destroy every mean of counterplay you have ( the point of this line is to sacrifice that bishop for h3 pawn ). Without your key attacker, your attack would be dead, while he would first bolster his kingside, and then demolish you on the queenside, since you would have no natural kingside counterplay.

Second thing, your tactical skill must be very high, because this line relies solely on that. There are no "hidden gems" it is raw and simple -> mate or die. You missed several times clean win which gave him the chance to win the game. I hope you will understand just how devastating blunders, or even imprecise moves, can be in this line, so please sharpen your tactical skill.

Third, play "clean chess". After 27.Kh2?? you had "clean victory" with 27...Rxg2+! 28.Nxg2 Qh3+ 29.Kg1 Qxg2# yet you decided to complicate with 27...Nxg2?? after which he could have won the game with 28.Rc7!!. Always aim to destroy opponent's counterplay, especially in these positions. If you have a queen and pawn versus rook, give the queen for the rook and win simple pawn endgame. In this opening, and in this mainline, there is always venom in position and "playing with fire" can backfire, in view of you stalemating the opponent or not seeing some cheap tactics. If you have a forced win, then by all means, win by force!


  When is it the right time to give up the rook on a8 for an attack on the king side?


Only if you are certain that your attack is so strong that it will at least get you perpetual check or will win you back the material. Other than that, forget it!

SUMMARY:

Well played, keep sharpening your tactical skill and learn theory to the last book move. Always aim to win the easiest way because this line is minefield and you never know when you will allow your opponent to play some crazy intermediate move that will give him magnificent comeback ( or maybe you will blunder a cheap tactical shot ).

If you have further questions leave a comment. Best regards.