Gummas are formed as a result of the body's immune response to the bacterium Treponema pallidum. The immune cells, including macrophages, lymphocytes, and plasma cells, aggregate to form granulomas. Over time, these granulomas can become fibrotic and necrotic, leading to the formation of a gumma.