In Genesis 4, Cain is Abel's older brother. He is put in charge of the fields ("fruits of the soil") and Abel is put in charge of the flocks.
It says in the text that while Cain brought "some of the fruits of the soil" as an offering to the Lord, his brother, Abel, "brought fat portions from some of the firstborn of his flock." (Genesis 4, NIV) Notice the difference in the offerings. Cain's is just an offering of something, he doesn't even consider giving the Lord the best of the fruit, let alone the first of the harvest. Abel does both. He gives the fattest of the firstborn animals.
Cain's afterthought of a sacrifice shows that he is not honoring God before everything else. He is acting selfishly.
Because of this, the Lord looks favorably on Abel and therefore Cain is jealous. When God says, "Sin is lurking at the door; its desire is for you, but you must master it," he is telling Cain, You have the opporunity to change your ways and win my favor - but you must overcome the temptation to be jealous of your brother. You must also offer me a sacrifice that comes from your heart and shows me how much you love me more than anything else in this world...
Instead of heeding this advice, Cain kills his brother in jealousy and rage.