You can get pills for a headache or cough medicine at any Apotheke (pharmacy) in Germany. Stronger medications, like for high blood pressure or diabetes, or antibiotics, are verschreibungspflichtig (they have to be prescribed by a doctor). After you've seen the doctor you will get a prescription that you can take to the pharmacy. You have to pay a fee for the prescription, called a "Zuzahlung". Refugees normally receive the medication free of charge and are partly supported by the relevant authority.
You can buy medication for less severe ailments, like cough medicine or painkillers, without a prescription. Pharmacists will normally offer information about non-prescription medicines and how you should take them. This information is also found on the instructions with the medication ("Beipackzettel"), like how long you should take the medicine, possible side effects, and how long the medicine will keep. If you are not clear about something, be sure to ask for help.