Drink plenty of water, and pee often. This helps to flush bacteria out of the bladder and urinary tract before it can settle in.
Wipe from front to back. Bacteria tend to hang around the anus. If you wipe from front to back, especially after a bowel movement, they're less likely to make it to the urethra.
Wash up before sex, and pee afterward. Use soap and water before sex. This keeps bacteria away from the urethra. And peeing afterward carries any bacteria that entered the urinary tract back out.