How does the formation of a blood clot occur?

Books are written. A great question with a long, complex answer. The short answer is something initiates it, often an injury, either occurring spontaneously or externally. This releases proteins that attract cells: platelets which adhere and aggregate. They in turn release active substances that begin a cascade of proteins that form fibrin which criss-crosses forming the clot.
Complex. There is complex interaction between blood platelets and the coagulation cascade, proteins in the blood that convert soluble fibrinogen into insoluble and sticky fibrin.