Legalities surrounding an incident I had with the police?

A while ago I had a health condition that worried my family into thinking I wasn't capable of driving a car safely, I disagreed. I was out driving and someone called the police to tell them. It is legal to drive in the condition I was in but apparently the police thought it wasn't safe either. I was sitting in my car in a parking lot and I bet they found me via tracking my iphone (I hadn't told anyone where I was going). As the cop showed up he parked so that our cars formed a T, to prevent me from backing out and leaving I assume. He did not turn on any lights or sirens but got out of his car and walked over to mine. He had me open my door and asked me some questions about what was happening (at the time I wasn't in the right mind to realize that I didn't have to answer his questions or open my door). At this point another cop arrived and I was asked to get out of my car. So I locked the doors and stepped out, as I was shutting my door the cop grabbed my hand (which was on the door) to stop me from shutting it and commanded me to give him the keys. Again, mindlessly I did everything he said and told him that I'm not allowing him to search my car. As I was standing there two cops opened my car doors and had the entire upper halves of their bodies in the car asking each other if they could see or smell anything. I was then cuffed and stuffed and taken to a hospital.

What exactly happened? Was that "search" legal? Was I arrested? Could I have refused to go?

It depends on the details but I would guess you were taken to the hospital because they believed you were a danger to yourself or others. That is not an arrest.

The police do have the authority to stop someone if they believe they are unsafe, as part of their public safety role, according to the Supreme Court.

What they did does constitute as a search. Whether are not it was legal depends on the circumstances. They may have had probable cause. There are also many exceptions to the probable cause rule, like consent. If the car is owned by your parents or you are under 18, your parents can give consent.