is the chimney brick? if so the brick needes to be groved with a diamond or carborinium blade at the height where the top of the flashing ends. this is so you can bend a small lip in the flashing to go in the groove of the brick. tar it in or use roof mastic. the water will run off the chimney and over the flashing not behind it. the roofing material will have to be removed from around the chimney to attach the flashing to the material under your shingles.
if your chimney is wood around a galvanized vent pipe you will need a flashing that forms a Z at the top and then just trim it out with a 1 X 4 making sure that no wood rests on your roof. if it does you could trap water causing the wood to rot.