Flaw's answer is of course correct, but here's another way to look at it.
Start with a simple sentence like this:
犬{いぬ}が好{す}きだ。 "I like dogs."
Since the predicate is a na-adjective, 好きだ, the object (犬) needs to be marked by が. (Your second sentence is ungrammatical for this reason, btw.)
Then, if you want to say something like "I like running.", you need to change the verb 走{はし}る into something like a noun, so it fits in the same spot as 犬. You do this with 〜の. This is essentially the same thing as "run" → "running" in English. So, we get:
走るのが好きだ。 "I like running."
If you instead have a predicate which requires an を-marked object, like やめる "to quit"/"to stop", it goes like this:
会社{かいしゃ}をやめた。"I quit my job."
走るのをやめた。 "I stopped running."
Basically, all the 〜の does is make a verb act like a noun, and then syntactically-speaking, that noun-like thing can fit into any spot a noun would normally go, and that spot will already be marked with が, の, or some other case particle.