There is no firm rule about whether you should paint the trim or walls first, except if you are spray-painting walls and ceilings. Your choice will depend on the quality of materials and what techniques and tools you are using.
Tape or Not
The newer forms of painter’s tape for use with latex paints work better to seal edges against the possibility of paint creeping under them. If you use this tape, you can paint the trim or walls in whatever sequence you want.
Make sure the painted surface is dry, or at least surface-cured, before you tape over it; otherwise, removing the tape could pull the paint off.
Walls Painted First
Painting walls first will keep splatters from getting on newly painted trim. If you are using quality -- not box store -- paint, brushes and roller covers, this should be a minimal concern.
Keep a rag dampened with water or mineral spirits handy for any mistakes. However, when spray-painting walls and ceilings, trim should always be painted last.
Cutting Walls Into Trim
A $15 or $20 pro-quality 25-inch or 3-inch sash brush will outlive most do-it-yourselfers' lifetime home ownerships. Don’t buy $10 bags of poorly constructed brushes that flow paint everywhere and shed bristles.
A professional painter can achieve straight lines without tape, cutting in a wall up to the trim. So can you, with practice.
However, it is harder to hold a straight line on a half-inch trim edge and cutting it to a wall. This is why most professional painters paint the trim first.
No one will notice a bit of wall color on a perpendicular trim.