Grammatical gender is not the same as biological sex. You could think of it as genders a, b, c, rather than masculine, feminine, neuter.
It is simply used as a classification system for nouns. Many languages use it. Different divisions are used like animate vs inanimate or masculine vs feminine. There is a lot of reading available on the subject.
It depends on the noun's gender and case.
If the noun is masculine: ein (subject), einen (direct object), einem (indirect object).
If the noun is feminine: eine (subject), eine (direct object), einer (indirect object)
If the noun is neuter: ein (subject), ein (direct object), einem (indirect object)