If the name is adjectival in form -- ending in -ski, -cki, -zki, -ny, -ly, and so on -- then the endings with -(i)ego and -(i)ej and -ich/-ych apply. If the name is a noun in form, different endings apply, depending on the grammatical gender and form of the noun. It gets complicated, but that's the fundamental rule.
Here the crucial point is that some surnames come from nouns -- Nowak, "new guy," Piekarczyk, "baker's boy," Janowicz, "son of Jan" -- and some are adjectives -- Maly, "little," Krakowski, "of/from Krakow," Ciezki, "heavy," and so on. Surnames that are adjectival in form express relationships with endings such as -ego, as in Janowskiego (of Janowski {male}), or -ej, as in Janowskiej, (of Janowski {female}), or -ich, as in Janowskich (of the Janowskis). Nouns use a different set of endings to say the same thing. "Of Nowak" is Nowaka, and "of the Nowaks" is Nowaków. There used to be distinct possessive forms for females, Nowakowej for "of Mrs. Nowak" and Nowakówny for "of Miss Nowak," but those are gradually becoming archaic.