Graphitic carbon nitride (g-C3N4) was obtained by thermal polymerization of dicyandiamide, thiourea or melamine at high temperatures (550 and 600 °C), using different heating rates (2 or 10 °C min−1) and synthesis times (0 or 4 h). The effects of the synthesis conditions and type of the precursor on the efficiency of g-C3N4 were studied. The most efficient was the synthesis from dicyandiamide, 53%, while the efficiency in the process of synthesis from melamine and thiourea were much smaller, 26% and 11%, respectively. On the basis of the results provided by X-ray diffraction (XRD), X-ray photoelectron spectroscopy (XPS), infrared spectroscopy (FTIR), ultraviolet–visible spectroscopy (UV–vis), thermogravimetric analysis (TGA), elemental analysis (EA), the best precursor and the optimum conditions of synthesis of g-C3N4 were identified to get the product of the most stable structure, the highest degree of ordering and condensation of structure and finally the highest photocatalytic activity. It was found that as the proton concentration decreased and the degree of condensation increased, the hydrogen yields during the photocatalytic decomposition of water–methanol solution were significantly enhanced. The generation of hydrogen was 1200 µmol g−1 and the selectivity towards hydrogen of more than 98%.