Why do some vegetables taste bitter?
Nice question! Many vegetables taste bitter because they contain compounds known as phytonutrients (Greek phyto = "plant"). There are more than 2500 phytonutrients known, and the most important (and common) ones are categorized as1:


Carotenoids: they are the ones that give red/orange/yellow color to some vegetables. They are mainly antioxidants, but some of them, like $\beta$-carotene and $\beta$-cryptoxanthin, are good for eyes as they can be converted into vitamin-A, whose derivative forms a part of phototransduction cascade.
Ellagic acid: it is found in a number of berries, such as strawberries, raspberries, pomegranates, etc. and has been shown in vitro to be helpful in slowing the growth of cancer cells and aid liver in neutralizing cancer-causing chemicals.
Flavonoids: they are found in a large variety of plants and constitute a large number of phytonutrients. Some flavonoids, like catechins, hesperidin and quercetin are very good for health. For example, see this answer for effects of catechins on the body.
Resveratrol: it is found in grapes, red wine, etc. and shows antioxidant and anti-inflammatory effects. Some researches have also concluded that it may reduce risk of heart disease and certain cancers.
Glucosinolates: these are found in cruciferous vegetables, like sprouts, cabbage, etc. and help checking formation and growth of cancer. This is the one we're talking about, so I'll continue about this below.
Phytoestrogens: they can exert estrogen-like effects on the body because of their structure, and even block body's natural supply of estrogen. Isoflavones can also reduce risk of endometrial cancer and bone loss in women. They are also found in a large number of plants, including soybeans, oats, barley, rice, apples, carrots, etc. (see full list here).


Another category, alkaloids, has been discussed by @WYSIWYG in their answer. These are all made naturally by plants as a part of their defense mechanism against insects and pests. This, in part, explains why they are bitter.

Coming to main point, glucosinolates are organic compounds that are responsible for bitter taste and pungent smell of many cruciferous vegetables. They are derived from glucose and amino acid, and contain sulfur and nitrogen as constituents. When they are cooked, chewed, or otherwise damaged, an enzyme myrosinase cleaves the glucose molecule, while the remaining molecule converts into isothiocyanate, thiocyanate and nitrile2, which cause the bitter taste and pungent smell (think of the isocyanide test you performed in grade XI and the smell it produced ;) The reaction looks like this: 

source

A special glucosinolate, sinigrin, has been shown as responsible for the bitterness of Brussels sprouts and cauliflower3. Also, high concentration of glucosinolates are toxic to humans as well as other animals, though tolerance levels vary even within the same genera (Acomys cahirinus and Acomys russatus)4. So, that partly explains why these compounds are bitter.

Talking about the anti-cancer properties of glucosinolates (going a bit off-topic just so that you know why these are good for you), two of the main compounds that are responsible for such effects are sulforaphane and indole-3-carbinol. Sulforaphane (in which R- becomes C5H11OS- in R-NCS), for example, is derived by the action of myrosinase on glucoraphanin (again, a glucosinolate). It has been shown to have anti-cancer properties. For example, Li et al, 2010 have shown that sulforaphane prevents breast cancer by inhibiting breast cancer stem cells and downregulating the Wnt/$\beta$-catenin pathway (yeah, there is something like cancer stem cell!). In another research, Hsu et al, 2011 showed that sulforaphane prevents prostate cancer by inhibiting HDACs (histone deacetylases, which interfere with normal functioning of tumor suppressor genes) and DNA methylation. This example was just to mention about the anti-cancer properties of glucosinolates in general.

Another class of compounds, called cucurbitacins, are produced in plants of Cucurbitaceae family. Unlike phytonutrients, cucurbitacins are considered a type of steroid since they are derived from cucurbitane and stored as glycosides5. Their derivatives are also found in other families like Rubiaceae, Rosaceae and even some marine mollusks, and, much like glucosinolates, they are also produced for defense against herbivores.

However, it is not sure whether only glucosinolates are the only compounds responsible for bitterness of vegetables. A study suggests that increased levels of calcium can also be responsible for bitterness of vegetables6, although its evolutionary cause and mechanism of action are unknown yet. Also, it may even depend on the person(!). If you are a supertaster (which has high chances to be true if you're woman or from Asia, South America or Africa), you will tend to be highly sensitive to bitterness of Brassica oleracca cultivars, such as kale, Brussels sprouts or cabbage7,8. Sensitivity to coffee, green tea, mushrooms, etc. is also known to increase. You can view a full list on Wikipedia.

References:

 1. Phytonutrients - WebMD

 2. Burow, M; Bergner, A; Gershenzon, J; Wittstock, U (2007). "Glucosinolate hydrolysis in Lepidium sativum--identification of the thiocyanate-forming protein.". Plant molecular biology. 63 (1): 49–61.

 3. Van Doorn, Hans E; Van Der Kruk, Gert C; Van Holst, Gerrit-Jan; Raaijmakers-Ruijs, Natasja C M E; Postma, Erik; Groeneweg, Bas; Jongen, Wim H F (1998). "The glucosinolates sinigrin and progoitrin are important determinants for taste preference and bitterness of Brussels sprouts". Journal of the Science of Food and Agriculture. 78: 30–38.

 4. Samuni Blank, M; Arad, Z; Dearing, MD; Gerchman, Y; Karasov, WH; Izhaki, I (2013). "Friend or foe? Disparate plant–animal interactions of two congeneric rodents". Evolutionary Ecology. 27 (6): 1069–1080.

 5. Jian Chao Chen, Ming Hua Chiu, Rui Lin Nie, Geoffrey A. Cordell and Samuel X. Qiu (2005), "Cucurbitacins and cucurbitane glycosides: structures and biological activities" Natural Product Reports, volume 22, pages 386-399

 6. Tordoff MG, Sandell MA. Vegetable Bitterness is Related to Calcium Content. Appetite. 2009;52(2):498-504. doi:10.1016/j.appet.2009.01.002.

 7. Dinehart, M.E.; Hayes, J.E.; Bartoshuk, L.M.; Lanier, S.L.; Duffy, V.B. (2006). "Bitter taste markers explain variability in vegetable sweetness, bitterness, and intake". Physiology &amp; Behavior. 87 (2): 304–13.

 8. Drewnowski, Adam; Henderson, Susan Ahlstrom; Levine, Alisa; Hann, Clayton (2007). "Taste and food preferences as predictors of dietary practices in young women". Public Health Nutrition. 2 (4).

 9. Li Y, Zhang T, Korkaya H, et al. Sulforaphane, a Dietary Component of Broccoli/Broccoli Sprouts, Inhibits Breast Cancer Stem Cells. Clinical cancer research : an official journal of the American Association for Cancer Research. 2010;16(9):2580-2590. doi:10.1158/1078-0432.CCR-09-2937.