I found the problem. The subroutine's code must go before glDrawArrays:
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
GLint ColorSelectorLoc;
GLuint colorBlueIndex;
GLuint colorGreenIndex;
ColorSelectorLoc = glGetSubroutineUniformLocation(program, GL_FRAGMENT_SHADER, "ColorSelector");
if (ColorSelectorLoc < 0)
{
fprintf(stderr, "Error: ColorSelector is not an active subroutine uniform in the shader\n");
exit(EXIT_FAILURE);
}
colorBlueIndex = glGetSubroutineIndex(program, GL_FRAGMENT_SHADER, "colorBlue");
colorGreenIndex = glGetSubroutineIndex(program, GL_FRAGMENT_SHADER, "colorGreen");
GLsizei n;
glGetProgramStageiv(program, GL_FRAGMENT_SHADER, GL_ACTIVE_SUBROUTINE_UNIFORM_LOCATIONS, &n);
GLuint *indices = new GLuint[n];
cout << "colorGreen: " << colorBlueIndex;
indices[ColorSelectorLoc] = colorGreenIndex;
glUniformSubroutinesuiv(GL_FRAGMENT_SHADER, n, indices);
delete [] indices;
glBindVertexArray(TriangleVAO);
glDrawArrays(GL_TRIANGLES, 0, NumVertices);
glFlush();
}