Basically affiliation is valid for employes to an organisation. Graduate students will belong to that category as well since their degree depends on producing publishable materials. Undergraduate students are not automatically entitled to use an affiliation. I do not think this introduces any majpor hurdles. You need to talk to the department chair or have some faculty member do so to get the permission to use the affiliation. I am assuming such affiliaiton will not need decisions higher up in the university bureaucracy; you need to find out, of course.
Affiliation will make publication easier than using a private address so getting to use an affiliation provides a stamp of approval with it which may work to your advantage. So, if I understand your final question right, there does not have to be any connection between the research you do and the affiliation. The affiliation simply means that the department/university supports your publication and thereby your efforts in research.
In this example it is evident that there is a responsibility associated with using the university name (for example as affiliation). It is clear that the affiliation cannot be used "as you wish" without possible repercusions, hence asking for permission is a natural first step. Particularly when the work is not done at the university.
Universities have been very relaxed about "brandinG" but trust me, they are becoming increasingly watchful. This is evident by studying the visual identity and similar branding policies of many respectable universities.