You get mods by hacking portals - the same way you get other items like weapons and resonators.
About 5% - 10% of the items you get will be mods.
The level of the portal you are hacking doesn’t affect the quality of the mods you get. You can get a Very Rare mod by hacking a level 1 portal or a level 8 portal.
Glyph-hacking gives you more items per hack, which means you get more mods, along with more of every other kind of item. Glyph-hacking a level 8 portal gives you up to 20 items, which might include multiple mods!
Enemy portals give you less items in general, but you can still get Very Rare mods from hacking enemy portals.