How can having sex right after your period can u get pregnant?

Depends. If you truly had a period, i.e., something that occurs 14 days after you ovulate (release an egg) and the egg is not fertilized, then you cannot get pregnant until you ovulate again. Unfortunately, 10 different women might have 10 different lengths of time from period to ovulation, although if they don't get pregnant, they will all have 14 days from ovulation to their next period.
Sex after period. "generally not" the uterine lining is essentially gone after a period. However - the only fixed timeframe in a normal menstrual cycle is from the first day of your period until 14 days - thats when ovulation occurs - that is your most fertile time - so if your periods run long - its possible that you might be close to that 14 day time - plus sperm can survive for up to 24 hours in the vagina.