Wikipedia article on the First Germanic Sound Shift
"Eight" doesn't come from "octagon" (which means "eight-cornered"), but "okto" ("eight"). The English "gh" represents a sound that no longer exists, but was a kind of fricative, a bit like the "ch" in German "Bach" or Scottish "loch" (it softened over the centuries, first to a soft gargle and then to completely silent).
The precise law governing this change is the Germanic Spirant Law. Usually, stops became fricative, but under the Spirant Law, a stop was followed by a t, only the first stop was turned into a fricative, leaving the t intact.