Phlegm which is coughed up never tastes good. However if it tastes really bad and also smells bad or is reddish/greenish/yellowish it could indicate a pneumonia (chest infection)
A lot of times it is due to an infection. Infections such as lung or sinuse can cause mucus to be coughed up. Also if you smoke, you may also cough up a bit.
The primary cause of expectorating foul-smelling or foul-tasting mucus is infection. The infection could be located in the lungs, the throat, or the nasal passages.
You could possibly be suffering from a sinus infection, bronchitis, or COPD. You need to see your doctor to find out for sure what's wrong and get a prescription.