What happens depends on the pathogen and not just because of what is maturing immunologically but because of other changes as well. Think about the microcephaply that Zika causes as a prenatal infection while postnatally it is most often of no major consequence. Certain germs cause problems in different age groups and not others precisely because of the course of immunologic maturation. Did the mother pass on significant amounts of specific IgG to a particular bug? A large part of the infant response is based on that and on the innate system. What happens with some specific pathogens is described here if you want details.
Definitely pre-natal. I don’t know the exact timing.
The “clonal deletion” of T- and B-cells that react to self-antigen takes place principally in the thymus, so it must be after that develops. The interesting thing, of course, is that many self-antigens are tissue-specific in their expression. I think the thymus expresses essentially every protein that’s present in any tissue in the body at this stage in order to expose the lymphocytes to all possible self antigens. Of course, autoimmune diseases are common, so this is presumably a complex and far-from-flawless process. It’s interesting that non-identical twins (with different MHC haplotypes) tend to be mutually tolerant of tissue grafts later in life. This implies that antigen-presenting-cells from one twin (i.e. with one twin’s MHC “encryption”) present self-antigen to the other twin’s T-cells during the period of clonal deletion.
As for defense against infection prior to the immune system going “live”, i.e. in the early fetus, I don’t know.
There’s another big complication in that the fetus has a different MHC haplotype to the mother, so as soon as antigen-presenting-cells in the fetus become active, they will be presenting self-antigens with a different “encryption key” that look like non-self to the mother’s immune system. So the placenta forms a barrier, supplemented by other mechanisms that prevent the mother’s immune system from attacking the fetus as though it were an incompatible tissue transplant. Some spontaneous miscarriages may be attributable to a failure in this barrier.