What affiliation is appropriate to list for journal publication? On journals for example this one, I am required to provide "the department and institution to which the work should be attributed" on the title page.

What does attributing the work to an institution mean? 

For my situation, I am a student at University of A, but I was approached by a researcher at Hospital B to finish the project, i.e. we are at two different institutions and labs. The project is mostly developing a signal processing algorithm and there was no particular funding source.

Sorry if this is a silly question as this is my first time submitting a paper myself.
The main purpose of listing your affiliation is to give some context for the work:


Speaking very generally, intellectual property is shared between the author (who understands and can reproduce the work) and the affiliation (which paid for it), so it's important to list both. 
Being affiliated with a well-known research group will lend credibility to your work; the quality of the paper will also reflect on the group.
Universities, hospitals, private companies, and the military have different priorities; listing the affiliation can clarify any biases.


As for knowing who to list, the main question is who is paying you? Who is paying for the research equipment, and your mentorship? 


If it is the university or hospital, you should list them. You may need to list them both if they are both paying for you in some capacity. Note that if you are receiving salary, all your work may belong to the university, even work which you do on your own initiative and separately from your normal responsibilities. 
If you are doing this for a class at the university, you should probably list the university (Whether you have an obligation to do this is a more interesting question). You may also want to add the instructor of the class as a co-author of the paper. 
If you are using your personal equipment and are not receiving any salary for this work (not even a graduate student stipend), and are not receiving mentorship, then you have no obligation to list anyone, though you may want to list the university just to get more credibility. 


The other thing to consider is that you are presumably using the hospital's data and transitioning to their system.


If someone from the hospital is a co-author, then great.
If not, you should probably acknowledge the hospital, or add them as an affiliation for yourself.