The flat rate applies to interest, dividends and realised gains, with a tax-free allowance that can be allocated to your broker so that small amounts are not withheld. German institutions deduct and remit automatically, so many investors file nothing extra.
Losses are ring-fenced into pots: losses on shares can generally only offset gains on shares, and rules limiting the offset of losses from forwards and derivatives have been introduced, contested and revised, which has been a live issue for active derivatives traders.
Accounts at foreign brokers are not subject to German withholding, so the income must be declared on a tax return, and foreign withholding tax credits are claimed there.
General information about Germany, not tax advice. Rules change and depend on your circumstances; take advice from a qualified German tax professional.
Related: bafin, esma, capital-loss-carryover, withholding-tax-w8ben, mifid-ii